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

Illustration
Mitchel Jay - 2024-03-21T21:55:30+00:00
Question: How do I get the max value within a specified range on my plot?

  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 answered . 2025-11-20

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

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!