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

Technical Source
2 min readMar 22, 2024

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));

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.