MATLAB Answers

0

Frequency spectrum of a sound signal

Lok Hua Lee さんによって質問されました 2019 年 10 月 20 日
最新アクティビティ Lok Hua Lee さんによって コメントされました 2019 年 10 月 23 日
Hi guys, I would like to know some hints on how to plot frequency spectrum of magnitude and phase spectra of an audio signal in both omega and frequency as x-axis parameter (plot separately). Thanks. My code is as below and i'm not sure what's going on.
[y,fs] = audioread('test.wave');
N = length(y);
t = (0:N-1/fs);
n = (0:N-1);
y = y(:,1);
% spectral analysis
w = hanning(N, 'periodic');
[X, f] = periodogram(y, w, N, fs, 'power');
X = 20*log10(sqrt(X)*sqrt(2));
% plot the signal spectrum
figure(1);
subplot(2,1,1);
semilogx(f, X, 'r');
xlim([0 max(f)]);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
title('Amplitude spectrum of the signal');
xlabel('Frequency, Cycles/Second');
ylabel('Magnitude, dB');
subplot(2,1,2);
semilogx((2*pi*f), X, 'r');
xlim([0 max(2*pi*f)]);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
title('Amplitude spectrum of the signal');
xlabel('Angular Frequency, Radian/Sample');
ylabel('Magnitude, dB');

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Kaashyap Pappu 2019 年 10 月 23 日

Using a test sound file of my own, I was able to generate the plots attached. The function “audioread” works if the suffix of the sound file name is ‘.wav’ not ‘.wave’.
A similar question has been addressed here.
Hope this helps!

  1 件のコメント

Lok Hua Lee 2019 年 10 月 23 日
Thanks for the reply, the following code is what i think should be working fine. But im not sure if the graph should be the same for both frequency and angular frequency?
clear, clc, close all;
[y,fs] = audioread('test.wav');
N = length(y); % Length of vector y, number of samples
Y = fft(y,N); % Fourier transform of y
F = ((0:1/N:1-1/N)*fs); % Frequency vector
w = 2*pi*F; % Angular frequency vector
magnitudeY = abs(Y); % Magnitude of the FFT
phaseY = unwrap(angle(Y)); % Phase of the FFT
figure (1);
subplot(2,1,1);
plot(F, magnitudeY);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
xlabel('Frequency, Hz');
ylabel('Magnitude, dB');
title('Magnitude spectrum of sound wave in frequency');
subplot(2,1,2);
plot(w, magnitudeY);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
xlabel('Angular Frequency, rad/sample');
ylabel('Magnitude, dB');
title('Magnitude spectrum of sound wave in angular frequency');
figure (2);
subplot(2,1,1);
plot (F, phaseY);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
xlabel('Frequency, Hz');
ylabel('Phase angle, radian');
title('Phase spectrum of sound wave in frequency');
subplot(2,1,2);
plot (w, phaseY);
grid on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14);
xlabel('Angular Frequency, rad/sample');
ylabel('Phase angle, radian');
title('Phase spectrum of sound wave in angular frequency');

サインイン to comment.



Translated by