2
votes

I'm trying to find the maximum peak on a power spectral density plot created in Matlab. I can create the plot just fine but am having difficulty correctly marking it. I use the find peaks and max function to find it but Matlab cannot correctly mark it. It finds the correct height but marks it a little to the left or right. Here is the code:

data = load ('EEGData(test1).txt', '-ascii');
figure(1)
plot(data)

Y =fft(data,251);

Pyy = Y.*conj(Y)/251;

f = 1000/251*(0:127);

figure(2) 
plot(f,Pyy(1:128))
title('Power spectral density')
xlabel('Frequency (Hz)')

[a,b] = findpeaks(Pyy(1:128));
MAX = max(a);
hold on
plot(f(b), MAX,'or')

any help would be greatly appreciated.

1

1 Answers

6
votes

When I tested your code as is by replacing data with

data=randn(251,1);

...I found that the local peak locations indicated by the red o markers were in the correct locations. It was just that all peaks were marked at the height of the maximum peak.

I'm not 100% sure what you are trying to do, but it looks as if you are trying to just find the maximum peak. If this is the case then you do not need the findpeaks function. Just replace the last few lines of your code with the following ...

[MAX, MAXidx] = max(Pyy(1:128));
hold on
plot(f(MAXidx), MAX,'or')