【问题标题】:Findpeaks in Spectrum Matalb频谱 Matlab 中的 Findpeaks
【发布时间】:2021-12-12 15:00:01
【问题描述】:

我试图在频谱中找到峰值,但我只需要提取基频峰值及其谐波,红色矩形。如何排除基频之前的任何内容,仅包括基频及其 3 个谐波。我使用此代码,但它没有帮助。有什么想法吗?

pks = findpeaks(q);
findpeaks(q,'MinPeakDistance',99)
%findpeaks(q,'MinPeakHeight',0.0004)
xlim([0.1 500])

使用时:

    Fs = 1000;
t = 0:0.001:1-0.001;    % 250-Hz sine wave modulated at 100 Hz
x = [1+cos(2*pi*100*t)].*cos(2*pi*250*t);
%envspectrum(x,Fs)
[ES,F]=envspectrum(x,Fs);

%%
findpeaks(ES,F)
% Now for only > 99 Hz (choose the freq you fancy)
idx = F >= 99; % greater than 99 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 99
% Good? Keep the values of  amplitud and location (in frequencies) of the ES
[pks,loc] = findpeaks(ES(idx),F(idx));
% If you just want to have the first 5 peaks (or the n? you choose):
% Select only 3 first.
if length(pks) > 3  % Check you didnt get less peaks
    pks = pks(1:3);
    loc = loc(1:3);
end
% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')

我明白了:

结果如下:

【问题讨论】:

  • 我使用findpeaks 函数绘制所有包络谱图,它发现峰值为 5、75 和 172 Hz 的频率,这是错误的。这些值似乎与它们的邻居相等。为什么findpeaks 将它们视为“峰值”?我不知道,函数的算法出了点问题。但这是一个内部问题,不是我的代码。由于高度是寻找假峰的问题,您可以使用一些参数来避免这种情况(下一个)
  • 您可以尝试 3 个选项:使用输入:'MinPeakHeight' 指定绝对幅度,'MinPeakProminence ' 指定相对幅度,'Threshold' 指定其邻居之间的相对差异。这 3 个输入中的任何一个都应该足以避免选择错误的峰值。另外我刚刚读到有一个特定的输入来说明你想要找到的峰值数量('NPeaks')。那我会编辑我的答案。供参考:es.mathworks.com/help/signal/ref/findpeaks.html#namevaluepairs
  • 另外,很明显峰值或你的 ES 是谐波,所以更快的方法:findpeaks(ES,F,'MinPeakDistance',99, 'Npeak', 3)。你做得很好。很抱歉造成混乱,但从一开始就没有示例会更加困难。
  • 谢谢,这适用于特定生成的信号,findpeaks(ES,F,'MinPeakDistance',99, 'Npeak', 3) 但它不适用于任何信号,我试过了。我也不能使用“MinPeakHeight”、“MinPeakProminence”或“Threshold”,因为只需要谐波幅度,它可能是非常接近谐波的周围峰值。
  • 这是一个更具体的问题,可能超出了我的范围。抱歉,我帮不上忙。

标签: matlab


【解决方案1】:

通过示例编辑以适应新代码。我们正在应用信号的包络谱 - [ES,F] = envspectrum(sig,Fs);-,因此我们知道信号及其频率采样 (fs)。

仍然是相同的过程。您可以仅针对高于 250 Hz 的信号样本计算 findpeaks() 中的值。为此,您可以为想要的频率值定义一个逻辑数组:

idx = F >= 250; % 250 Hz

并将此逻辑索引应用于您想要应用函数findpeaks() 的信号和频率的包络频谱:

[pks,loc] = findpeaks(ES(idx),F(idx));

例子:

% Let's create a signal with fs = 2500 Hz
% This is just to create an example, don't worry about these lines
fs = 2500;
f0 = 25;
n = 8;
d = 0.02;
p = 0.12;
t = 0:1/fs:1-1/fs;
z = [1 0.5 0.2 0.1 0.05]*sin(2*pi*f0*[1 2 3 4 5]'.*t)/5;
% z: our signal
% t: time array of the signal (to plot)

% How does the signal look?
plot(t,z)

% Calculating the envelope signal and its spectrum
[ES,F]=envspectrum(z,fs);
% Plot the envelop if you want:
envspectrum(z,fs)

% ES: Envelope spectrum of the signal
% F: array of frequencies used for the spectrum (half our fs for Nyquist Theorem)

% Find the peaks of the envelope spectrum
   % Let's see how it is for all the envelope
findpeaks(ES,F)
   % Now for only > 250 Hz (choose the freq you fancy)
idx = F >= 250; % greater than 250 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 250
   % Good? Keep the values of  amplitud and location (in frequencies) of the ES
      % Use 'NPeaks' input to tell function to select only 5 first peaks it can find.
[pks,loc] = findpeaks(ES(idx),F(idx), 'NPeaks', 5);

% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')
% Note in this example the first peak (in 250 Hz) is not selected 
% because is not a local maximum when we narrow the envelope.

 % If the function select false peaks somehow, you can use
 %'MinPeakHeight' to specify an absolute amplitude, 'MinPeakProminence '
 %for a relative amplitude or 'Threshold' inputs for a better fit.

https://uk.mathworks.com/matlabcentral/answers/768537-use-findpeaks-on-specific-frequency-range

https://uk.mathworks.com/help/signal/ref/findpeaks.html#responsive_offcanvas

【讨论】:

  • 对不起,我不能让它工作。我正在使用此代码 [ES,F] = envspectrum(sig,Fs);得到如上图。如何以赫兹为单位?
  • 好吧,我不知道。让我编辑帖子以向您展示该功能。但是,findpeaks() 函数的使用方法是一样的。 @user33834
  • 谢谢。这应该有效,但这部分不起作用:如果长度(pks)> 5:%检查你没有得到更少的峰值 pks = pks[1:5];位置 = 位置 [1:5];结束我得到这个错误:如果长度(pks)> 5:%检查你没有得到更少的峰值↑错误:无效的表达式。检查缺失或多余的字符。
  • 对,我对 Python 感到困惑 :) 我已经有一段时间没有为 Matlab 写东西了。我会改变它。如果这是您需要的,请检查答案为绿色勾号;)@user33834
  • 谢谢RobertoT,但抱歉还是不行:(
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 2015-06-12
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多