MATLAB: find peaks from data iterations

时间:2016-04-04 16:50:32

标签: matlab

I have a function that plots the magnitude of an fft function from a signal.

For every iteration I want to determine the x-value of the two peaks below 2000. I thought this was relatively simple using the function findpeaks however it has not given me the correct output.

I do not intend to plot the ouput, but just for illustration purposes here is a plot. I only want to know the peaks for the data below 2000 (the first set of peaks)

enter image description here Example of one iteration:

example of one iteration

Here is a bit of my code. B is a vector containing the starting indices for every segment of data that needs to be analysed.

function [number] = fourir_(data,sampling_rate)

%Finds the approximate starting index of every peak segment
%B is a vector containing the indeces
[A,B] = findpeaks(double(abs(data) > 0.6), 'MinPeakDistance', 2500); 

Fs = sampling_rate;
t = 0:1/Fs:0.25;

C = zeros(size(B),2)
   for i = 1:numel(B)
    new_data = data(B(i):(B(i)+200))
    y = double(new_data)/max(abs(new_data));
    n = length(y);
    p = abs(fft(y));
    f = (0:n-1)*(Fs/n);

   end

Example data: https://www.dropbox.com/s/zxypn3axoqwo2g0/signal%20%281%29.mat?dl=0

2 个答案:

答案 0 :(得分:0)

The following may help, which seems to get the peaks from one fft of your signal data,

clear all
close all

%load sample data from https://www.dropbox.com/s/zxypn3axoqwo2g0/signal%20%281%29.mat?dl=0
load('./signal (1).mat')

%get an FFT and take half
p = abs(fft(signal));
p = p(1:length(p)/2);

%find peaks and plot
[pk, loc] = findpeaks(p,'MINPEAKHEIGHT',100,'MINPEAKDISTANCE',100);

plot(p,'k-')
hold all
plot(loc, pk, 'rx')

which looks like,

enter image description here

Where some of the peaks are isolated...

答案 1 :(得分:0)

Here is your answer, this is exactly what @Ed Smith suggested in his first comment. You can just add a threshold in order to distinguish the major peak.

%Finds the approximate starting index of every peak segment
%B is a vector containing the indeces
[A,B] = findpeaks(double(abs(data) > 0.6), 'MinPeakDistance', 2500); 

Fs = sampling_rate;
t = 0:1/Fs:0.25;

C = zeros(size(B),2)
   for i = 1:numel(B)
    new_data = data(B(i):(B(i)+200))
    y = double(new_data)/max(abs(new_data));
    n = length(y);
    p = abs(fft(y));
    f = (0:n-1)*(Fs/n);
    p1 = p(1:round(length(p)/2)); 
    p1(p1<10) = 0; %add a threshold
    [~,ind] = findpeaks(p1); %index of where are the peaks
    C(i,:) = f(ind);
    hold on
    plot(f,p,'b',C(i,:),p(ind),'ro')
   end