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

조회 수: 7 (최근 30일)
Mitchel Jay
Mitchel Jay 2016년 4월 11일
편집: Mariraja Ponraj 2019년 10월 8일
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));

채택된 답변

dpb
dpb 2016년 4월 12일
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
dpb
dpb 2016년 4월 12일
Yeah, sorry. Don't want f vector, want df for the delta frequency to locate the bin number.
Mitchel Jay
Mitchel Jay 2016년 4월 19일
Got it working properly after switching a few values and variables around. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mariraja Ponraj
Mariraja Ponraj 2019년 10월 8일
편집: Mariraja Ponraj 2019년 10월 8일
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.
matlab_query.jpg

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by