필터 지우기
필터 지우기

Can the amplitude be plotted in dB?

조회 수: 11 (최근 30일)
Taylor Gray
Taylor Gray 2019년 3월 12일
댓글: Star Strider 2020년 7월 21일
In the code and figure below I have located peaks in an audio signal using the frequency domain and I can't figure out how to have the amplitude plotted in dB. Could anyone help me please?
[audio_file, Fs] = audioread("Handgun.wav");
L = size(audio_file,1);
Ts = 1/Fs; % Sampling Interval
Fn = Fs/2; % Nyquist Frequency
FT_af = fft(audio_file)/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
[PksL,LocsL] = findpeaks(abs(FT_af(Iv,1))*2, 'MinPeakHeight',0.006, 'MinPeakDistance',20);
[PksR,LocsR] = findpeaks(abs(FT_af(Iv,2))*2, 'MinPeakHeight',0.006, 'MinPeakDistance',20);
figure(1);
semilogx(Fv, abs(FT_af(Iv,2))*2), xlabel('Frequency, Hz'),ylabel('magnitude');
hold on
plot(Fv(LocsR), PksR, '^r', 'MarkerFaceColor','r')
hold off
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Left Channel')

채택된 답변

Star Strider
Star Strider 2019년 3월 13일
Try this:
semilogx(Fv, 20*log10(abs(FT_af(Iv,2))*2)), xlabel('Frequency, Hz'),ylabel('magnitude');
That will convert the amplitudes to dB. The y-axis will reflect those changes, however the tick marks will not be in integer powers-of-10 such as with your x-axis and the semilogx call. You will have to format the y-axis ticks yourself.
  댓글 수: 2
Brad
Brad 2020년 7월 21일
Hi Star
Why is the multiple by 2 there?
Isn't this dB simply 20 log10(y)? (as the function mag2db) is defined?
Thank you
Star Strider
Star Strider 2020년 7월 21일
Brad — The fft function creates a symmetric result, with the second half being the complex-conjugate of the first half, the total signal energy being divided approximately evenly between the two halves. In a single-sided Fourier transform, it is then necessary to multiply the abs() of the result by 2 in order to approximate the original signal amplitude. Here, I multiply by 2 prior to the dB transformation.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by