What do I need to add to my code when using loglog to plot?

2 ビュー (過去 30 日間)
Taylor Gray
Taylor Gray 2019 年 3 月 14 日
回答済み: Andrea Monfredini 2019 年 3 月 14 日
I have used semilogx and semilogy to plot the attached audio signal but never loglog. I keep getting an error returned to me about the input arguments but am unsure on what to add in my code for it to work with findpeaks. Could anyone one advise 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(20*log10(abs(FT_af(Iv,2))*2), 'MinPeakHeight',-90, 'MinPeakDistance',50);
[PksR,LocsR] = findpeaks(20*log10(abs(FT_af(Iv,2))*2), 'MinPeakHeight',-90, 'MinPeakDistance',50);
figure(1);
loglog(Fv, 20*log10(abs(FT_af(Iv,2))*2)), xlabel('Frequency, Hz', ylabel('Amplitude, dB'));
hold on
plot(Fv(LocsR), PksR, '^r', 'MarkerFaceColor','r')
hold off
xlabel('Frequency (Hz)')
ylabel('Amplitude (dB)')
title('Handgun')
When I run the code I get this error:
>> Spectral_Analysis_Handgun
Error using xlabel (line 21)
Incorrect number of input arguments.
Error in Spectral_Analysis_Handgun (line 11)
loglog(Fv, 20*log10(abs(FT_af(Iv,2))*2)), xlabel('Frequency, Hz', ylabel('Amplitude, dB'));

採用された回答

Andrea Monfredini
Andrea Monfredini 2019 年 3 月 14 日
Hi Taylor Grey,
the mistake is in the line when you call the loglog function.
loglog(Fv, 20*log10(abs(FT_af(Iv,2))*2)), xlabel('Frequency, Hz', ylabel('Amplitude, dB'));
you are using the function ylabel as an argument for xlabel, while instead you should separate those commands.
loglog(Fv, 20*log10(abs(FT_af(Iv,2))*2)); xlabel('Frequency, Hz'); ylabel('Amplitude, dB');
Given the fact that you also call xlabel and ylabel later in the script, i would suggest you to just keep the last xlabel and ylabel calls in the following way:
[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(20*log10(abs(FT_af(Iv,2))*2), 'MinPeakHeight',-90, 'MinPeakDistance',50);
[PksR,LocsR] = findpeaks(20*log10(abs(FT_af(Iv,2))*2), 'MinPeakHeight',-90, 'MinPeakDistance',50);
figure(1);
loglog(Fv, 20*log10(abs(FT_af(Iv,2))*2));
hold on
loglog(Fv(LocsR), PksR, '^r', 'MarkerFaceColor','r')
hold off
xlabel('Frequency (Hz)')
ylabel('Amplitude (dB)')
title('Handgun')

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by