如何解决在Octave中出现的findpeaks错误

huangapple go评论55阅读模式
英文:

How can I solve findpeaks error in Octave

问题

I see you're facing issues with the MATLAB code in Octave, particularly with the findpeaks function. To address the error and sorting problems, you can try the following steps:

  1. Check Octave Version: Make sure you are using an Octave version that supports the findpeaks function. It should be at least version 6.2.0 for proper compatibility.

  2. Correct Parameter Names: In Octave, the parameter names for findpeaks are case-sensitive and should be in lowercase. Update your code as follows:

    Replace:

    [pks locs] = findpeaks(Pmusic, "DoubleSided");
    

    With:

    [pks, locs] = findpeaks(Pmusic, "DoubleSided");
    

    Similarly, update other parameter names in your code to lowercase where necessary.

  3. Sorting Peaks: To get more than 2 DOA estimates and properly sort the peak values, you can use the following code:

    [pks, locs] = findpeaks(Pmusic, "DoubleSided", "MinPeakHeight", 5);
    [sorted_pks, sorted_idx] = sort(pks, 'descend');
    top_K_idx = locs(sorted_idx(1:K));
    MUSIC_Estim = theta(top_K_idx);
    

    This code will find peaks with a minimum height of 5 dB, sort them in descending order, and then select the top K peaks.

Make sure you have Octave updated to a compatible version and apply these code modifications. It should help you resolve the issues you mentioned.

英文:
theta = -90:0.01:90;      
for i=1:length(theta)
    SS = zeros(Nr,1);
    SS = exp(-1j*2*pi*d*(0:Nr-1)'*sind(theta(i))/lambda);
    PP = SS'*(Vn*Vn')*SS;
    Pmusic(i) = 1/ PP;
end
Pmusic = real(10*log10(Pmusic)); %Spatial Spectrum function
[pks,locs] = findpeaks(Pmusic,theta,'SortStr','descend','Annotate','extents');
MUSIC_Estim = sort(locs(1:K))

I want to run MUSIC matlab code in Octave (because I don't have matlab)

in matlab free 30day edition, It does work well but not in Octave.

>> error: findpeaks: non-string for Parameter name or Switch

how can I solve this error? I downloaded signal library and changed but it didn't work.

In Octave, provided matlab example works well. why doesn't work above code?

t = 2*pi*linspace(0,1,1024)';
 y = sin(3.14*t) + 0.5*cos(6.09*t) + 0.1*sin(10.11*t+1/6) + 0.1*sin(15.3*t+1/3);

 data = abs(y + 0.1*randn(length(y),1)); # Positive values + noise
 [pks idx] = findpeaks(data,"MinPeakHeight",1);

 dt = t(2)-t(1);
 [pks2 idx2] = findpeaks(data,"MinPeakHeight",1,...
                              "MinPeakDistance",round(0.5/dt));

 subplot(1,2,1)
 plot(t,data,t(idx),data(idx),'or')
 subplot(1,2,2)
 plot(t,data,t(idx2),data(idx2),'or')
plot(t,data,t(idx2),data(idx2),'or')

please help. thanks.

I tried

>> pkg install "https://github.com/gnu-octave/pkg-control/releases/download/control-3.5.2/control-3.5.2.tar.gz"

>> pkg install "https://downloads.sourceforge.net/project/octave/Octave%20Forge%20Packages/Individual%20Package%20Releases/signal-1.4.3.tar.gz"

>> pkg load control

>> pkg load signal

in Octave command window but it didn't work.

This MUSIC algorithm matlab code is not written by me, but it's just a short calculator code, and I'll upload the entire code rather than write the code of the smaller version to see the value of MUSIC_Estim accurately.

DOA = [35 30];      %Direction of arrival (Degree)
T   = 200;         %Snapshots (or Samples)
K   = length(DOA); %The number of signal source(or traget)
Nr  = 9;          %Number of receiver's antennas
lambda = 1;      %Wavelength
d   = lambda/2;    %Receiver's antennas spacing
SNR = 10;           %Signal to Noise Ratio (dB)
A = zeros(Nr,K);   %Steering Matrix


for k=1:K
    A(:,k) = exp(-1j*2*pi*d*sind(DOA(k))*(0:Nr-1)'/lambda);
%Assignment matrix
end
Vj = diag(sqrt((10.^(SNR/10))/2));
s = Vj* ( randn(K,T) + 1j*randn(K,T) );
noise = sqrt(1/2)*(randn(Nr,T)+1j*randn(Nr,T));

X = A*s;
X = X+noise;      %Insert Additive White Gaussain Noise (AWGN)

% MUSIC (MUltiple SIgnal Classification)
Rx = cov(X');                     %Data covarivance matrix
[eigenVec,eigenVal] = eig(Rx);    %Find the eigenvalues and eigenvectors of Rx
Vn = eigenVec(:,1:Nr-K);          %Estimate noise subspace (Note that eigenvalues sorted ascendig on columns of "eigenVal")
theta = -90:0.01:90;       %Grid points of Peak Search
for i=1:length(theta)
    SS = zeros(Nr,1);
    SS = exp(-1j*2*pi*d*(0:Nr-1)'*sind(theta(i))/lambda);
    PP = SS'*(Vn*Vn')*SS;
    Pmusic(i) = 1/ PP;
end
Pmusic = real(10*log10(Pmusic)); %Spatial Spectrum function
[pks locs] = findpeaks(Pmusic, "DoubleSided");
locs = theta(locs);
MUSIC_Estim = sort(locs)

figure;
plot(theta,Pmusic,'-b',locs(1:K),pks(1:K),'r*'); hold on
text(locs(1:K)+2*sign(locs(1:K)),pks(1:K),num2str(locs(1:K)'))

xlabel('Angle \theta (degree)'); ylabel('Spatial Power Spectrum P(\theta) (dB)')
title('DOA estimation based on MUSIC algorithm ')
xlim([min(theta) max(theta)])
grid on

///
locs = theta(locs);
and
MUSIC_Estim = sort(locs)

value is not peak. so I tried

Pmusic = real(10*log10(Pmusic)); %Spatial Spectrum function
[pks,locs] = findpeaks(Pmusic, "DoubleSided","MinPeakHeight", 5);
[sorted_pks, sorted_idx] = sort(pks(1:K));  % Sort peaks in descending order
MUSIC_Estim = theta(locs(sorted_idx(1:K)))      % Select top K peak locations from sorted peaks

figure;
plot(theta,Pmusic,'-b',MUSIC_Estim,pks(1:K),'r*'); hold on
text(MUSIC_Estim(1:K)+2*sign(sorted_idx(1:K)),pks(1:K),num2str(MUSIC_Estim(1:K)'))

like this but it has another problem of sorting peak values and can't get more than 2 DOA.

答案1

得分: 1

Here is the translated content:

基本上,Octave findpeaks 支持一个用于数据的参数:[pks, loc] = findpeaks(data) 返回峰值在 pks 中,它们的索引在 loc 中。

Matlab findpeaks 有一个可选的额外输入参数,包含数据样本的横坐标 [pks, loc] = findpeaks(data,x)。在输出中,loc 包含峰值的横坐标而不是索引。

但这不是唯一的区别。Octave 版本没有 "SortStr" 属性。但是由于之后要进行排序,因此您不需要它。它也没有 "Annotate" 属性,但您也不需要它(根据 Matlab 文档:“如果使用输出参数调用 findpeaks,则将忽略此参数。”)

最后,在您的第一个示例中,您必须将 findpeaks 调用替换为以下两行代码:

[pks,locs] = findpeaks(Pmusic);
locs = theta(locs)   % 用 theta 值替换索引

请注意,您只需阅读文档就可以获得所有这些信息。

英文:

Basically Octave findpeaks supports a single argument for the data: [pks, loc] = findpeaks(data) return the peak values in pks, and their indexes in loc.

Matlab findpeaks has an optional extra input argument that contains the abscissas of the data samples [pks, loc] = findpeaks(data,x). On output, loc contains the abscissas of the peaks instead of the indexes.

But it's not the only difference. The Octave version doesn't have the "SortStr" property. But you don't need it as you are sorting afterwards anyway. It doesn't have either the "Annotate" property, but you don't need it either (from Matlab doc: "This argument is ignored if you call findpeaks with output arguments.")

At the end, in your first exemple you have to replace the findpeaks call by these two lines:

[pks,locs] = findpeaks(Pmusic);
locs = theta(locs)   % replaces the indexes by the theta values

Note that you could have got all of this by just reading the documentations.

huangapple
  • 本文由 发表于 2023年5月15日 15:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定