How do I get the max value within a specified range on my plot?
    7 views (last 30 days)
  
       Show older comments
    

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));
0 Comments
Accepted Answer
  dpb
      
      
 on 12 Apr 2016
        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
3 Comments
  dpb
      
      
 on 12 Apr 2016
				Yeah, sorry. Don't want f vector, want df for the delta frequency to locate the bin number.
More Answers (1)
  Mariraja Ponraj
 on 8 Oct 2019
        
      Edited: Mariraja Ponraj
 on 8 Oct 2019
  
      Hi, I am having a similar problem, I need to plot the max between the range 20 to 40, and 40 to 70. The data has been imported from excel and x ranges between 1 to 70. I need to plot the max value in y between the x range I have mentioned.Could some one help me.

0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

