Question
I am having trying to plot the fft and its peaks on the same plot. I have read that passing the frequency vector in findpeaks will allow the second argument to be in frequency so i have done that in the code i provided below on line 13. However, I am having a hard time plotting the peaks and the fft on the same plot. If someone could point out how to plot this that would be appreciated. %take the fourier transform xdft = fft(x); xdft = xdft(1:length(x)/2+1); % create a frequency vector freq = 0:Fs/length(x):Fs/2; % plot magnitude figure(2) semilogx(freq,20*log10(abs(xdft))); xlabel('f(Hz)'); title('FFT of signal'); %locs is now in frequency [pks,frqs] = findpeaks(abs(X),freq); figure(3) plot(xdft(locs), pks, 'or')
Expert Answer
Prashant Kumar
PhD Expert
Answered Nov 20, 2025
You did not provide ‘x’ or any of the parameters (such as ‘Fs’) so it is not possible to run your code, and you could easily have posted your code as an attachment here instead of pastebin.
That aside, try this:
[pks,locs] = findpeaks(20*log10(abs(xdft));
figure(2)
semilogx(freq,20*log10(abs(xdft)))
hold on
plot(freq(locs), pks, 'or')
hold off
xlabel('f(Hz)');
title('FFT of signal')
It is necessary to use the same values in the plot as in the findpeaks arguments.
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related FFT Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →