How do I get the max value within a specified range on my plot?

Mitchel Jay · Mar 21, 2024 · 2K views
Question
  I am writing a program that can read in audio files of recorded guitar notes in standard tuning (e,B,G,D,A,E), plot the signal and the frequency response, then compare the frequency value from the plot with the needed frequency to be in tune. The way I was doing it, was finding the max value or highest peak from the frequency response plot, then comparing it to a specified value needed for it to be in tune. I hit a problem when I got to the G string, because as you can see from the plot, the highest peak is not the first peak, and that is where the frequency I need lies (since a G3 note has to have a frequency of 196 to be in tune), so my method doesn't work. How can I get the max y value between the 100 & 300 frequency range, then get the x value related to that max value?   %Tuning for G String gG = 196.00; %Fourier Transform of G3 On Guitar [y,Fs] = audioread('3rd_String_G_vbr.mp3'); Nsamps = length(y); t = (1/Fs)*(1:Nsamps); %Prepare time data for plot %Do Fourier Transform y_fft = abs(fft(y)); %Retain Magnitude y_fft = y_fft(1:Nsamps/2); %Discard Half of Points f = Fs*(0:Nsamps/2-1)/Nsamps; %Prepare freq data for plot %Find max y value of the highest peak in the range, then find corresponding x %value [maxYValue, indexAtMaxY] = max(y_fft); xValueAtMaxYValue = f(indexAtMaxY(1));  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 21, 2026
Use the sampling frequency to find the locations with which to bound the search --
 
 
HiLo=[100 300];       % frequency bounds
nHiLo=round(HiLo/f);  % bounding locations
[maxYValue, indexAtMaxY] = max(y_fft(nHiLo(1):nHiLo(2));
indexAtMaxY=indexAtMaxY+nHiLo(1);  % offset computation

 

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

Get a Free Consultation or a Sample Assignment Review!