I need help with Matlab - FFT - getting pairs of peaks-freq in array - urgently need help

Dear all,
I need help with Matlab - FFT of electrical signal in order to get pairs of peaks-freq in array.
After that store those data into file which will be used afterwards to analyze new signal in the way of comparing amplitude of peaks harmonics.
That is small experiment in order to support master thesis I am working on it.
Optionaly I am ready to pay to someone who will support me in this.
here is starting code
Fs = 40000; % Sampling frequency 40kHz je dovoljno i za 40ti harmonik
T = 1/Fs; % Sampling period
L = 80000; % Length of signal
t = (0:L-1)*T; % Time vector
X = 2.7*sin(2*pi*50*t) + 1.5*sin(2*pi*150*t) + 0.6*sin(2*pi*250*t);
Y = fft(X); % Compute the Fourier transform of the signal
P2 = abs(Y/L); % Compute the two-sided spectrum P2.
P1 = P2(1:L/2+1); % Then compute the single-sided spectrum P1 based on P2
P1(2:end-1) = 2*P1(2:end-1); % and the even-valued signal length L.
- If here anyone have explanation please provide to me
figure(1)
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
axis([0 300 ylim])
figure(2)
[pks, locs]=findpeaks(P1, 'MinPeakDistance',50, 'minpeakheight',0.01);
plot(P1(locs), pks, 'or')
MFFT=[pks, locs];
m = size(pks);
n = size(locs);
disp(m);
disp(n);
disp(MFFT);
I added findpek function but output is like this:
2.7000 1.5000 0.6000 101.0000 301.0000 501.0000 - 1st 3 values coresponding to amplitudes and thats OK but last 3 values should show correct frequencies but thats not the case.
Do anyone have sugggestion or idea whats wrong in the implementation of findpeak function?
Many thanks in advance.

2 commentaires

Answers is NOT a pay for support site. It is a purely volunteer forum. It is not even appropriate to advertise for help where you offer to pay for tht support, since that makes Answers into a for fee consulting service.
Mike Pierie
Mike Pierie le 22 Août 2022
Modifié(e) : Mike Pierie le 22 Août 2022
OK, Ive got it. Sorry for such kind of advertisement. I put the code if anyone willing to help will be really welcomed as it is pretty urgent now. I changed the subject.

Connectez-vous pour commenter.

 Réponse acceptée

I think you need to specify the x locations (frequency in your case) in the findpeaks function, otherwise it just gives you the index number.
[pks, locs]=findpeaks(P1,f, 'MinPeakDistance',50, 'minpeakheight',0.01);

11 commentaires

I tried but didnt work.
Most important is whats going on through the FFT and where those data are going? In some array or matrix?
It should be easy but my knowledge is pure.
It does work. The output of the locations is at 50, 150, and 250 Hz, which are the harmonics in X:
X = 2.7*sin(2*pi*50*t) + 1.5*sin(2*pi*150*t) + 0.6*sin(2*pi*250*t);
Are you trying to find peaks in the FFT or in the time-domain signal? The discrete Fourier transform converts time domain data to the frequency domain. I'm not sure what you are trying to uncover with the findpeaks command if not those frequencies.
Mike Pierie
Mike Pierie le 22 Août 2022
Modifié(e) : Mike Pierie le 22 Août 2022
Thanks again, I am trying to get array or matrix from FFT results with pairs of peaks and corespodent frequencies.
Thats main goal now.
When I tried to see whats the output of the fft I got this:
fprintf(' %d', P1);
fprintf('\n');
1.422681e-17 9.641351e-16 1.265246e-15 6.314322e-16 3.006869e-16 2.700000e+00 6.424175e-16 1.073...
Those are just peaks. But where frquencies are?
you mean something like:
>> yourMatrix = [pks.',locs.']
yourMatrix =
1.0e+02 *
0.027000000000000 0.500000000000000
0.015000000000000 1.500000000000000
0.006000000000000 2.500000000000000
Okay. Each of those elements corresponds to the frequencies defined in that 'f' variable in your code. I think what you want is:
yourMatrix = [f.', P1.'];
You might want to read up on the discrete Fourier transform if some of this isn't clear. The values of P1 are is the single-sided amplitude spectrum.
Ahh this is something :-) Many thanks!
Finally Ive got the matix!
Ill have more questions but for this beggining many many thanks!
Great. Can to accept the answer?
Sure Ill do, just will be more questions. Can I also say thanks somewhere or thats going over "answer the question" only?
If you have other questions, it is best to post them separately. This helps others in the future sort out specific questions. You can always put a link in one question back you your last question. And yes, just accepting the answer is best.
Many thanks again! Ill do as you said.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Fourier Analysis and Filtering dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by