在matlab中查找指定范围内的峰值

时间:2013-09-20 12:28:19

标签: matlab

我正在尝试分析音频文件,因此我生成了信号的FFT,发现峰值高于某个阈值。 这是代码的一部分:

%---------------------------Performing FFT--------------------------------%
 for i = 2:No_of_peaks

    song_seg = song(max_col(i-1):max_col(i)-1);
%     song_seg = song(max_col(6):max_col(7)-1);
    L = length(song_seg);    
    NFFT = 2^nextpow2(L); % Next power of 2 from length of y

    seg_fft = fft(song_seg,NFFT);%/L;

    N=5;Fst1=50;Fp1=60; Fp2=1040; Fst2=1050;

%     d = fdesign.bandpass('N,Fst1,Fp1,Fp2,Fst2');
%     h = design(d);
%     seg_fft = filter(h, seg_fft);

%     seg_fft(1) = 0;
%     
    f = FS/2*linspace(0,1,NFFT/2+1);
    seg_fft2 = 2*abs(seg_fft(1:NFFT/2+1));
    L5 = length(song_seg);

    figure(1+i)
    plot(f,seg_fft2)
    title('Frequency spectrum of signal')
    xlabel('Frequency (Hz)')
    %xlim([0 2500])
    ylabel('|Y(f)|')
    ylim([0 300])

    [points, loc] = findpeaks(seg_fft2,'MINPEAKHEIGHT',20)
 end

现在我有了pointsloc我希望能够提取60Hz到1000Hz之间的峰值。如何编写代码来实现它并存储峰值幅度以及相应的频率值

提前完成

2 个答案:

答案 0 :(得分:2)

您可以使用:

PeakFinder

另一种更简单的方法是制作函数的一阶导数(使用具有离散频率的diff矢量),然后定位峰值。振幅可以从原始信号中获取。

答案 1 :(得分:2)

两种类似的方式来解决这个问题。首先,在FFT之后,选择包含感兴趣区域的光谱子集,并将峰值搜索例程应用于该部分:

 for i = 2:No_of_peaks

    % ... code steps where you fft etc here ...
    % ... now pick the region of interest

    fmin = 60;
    fmax = 1000;
    region_of_interest = fmax>f & f>fmin;
    froi = f(region_of_interest);  

    [points, loc] = findpeaks(seg_fft2(region_of_interest),'MINPEAKHEIGHT',20)

    % index into froi to find the frequency of the peaks
    fpeaks = froi(loc);

 end

一个非常类似的选择是选择峰值,然后选择感兴趣的频率区域内的子集,如下所示:

 for i = 2:No_of_peaks

    % lots of code here to fft etc ...

    [points, loc] = findpeaks(seg_fft2,'MINPEAKHEIGHT',20)

    % find the peaks within the region of interest 

    fmin = 60;
    fmax = 1000;
    peaks_of_interest = find(fmax>f(loc) & f(loc)>fmin);
    fpeaks = f(loc(peaks_of_interest));
    apeaks = points(loc(peaks_of_interest));  % magnitude of the peaks


 end