Plotting the magnitude and phase spectra of a wav file in the range of -fs/2 to fs/2

耗尽温柔 提交于 2019-12-04 02:15:36

问题


I'm having problems plotting the FFT of a wav file. I managed to plot the magnitude and phase spectrums of the signal, however I need to repeat this in range -fs/2:fs/2.

%read sound files 
%'y' is the vector holding the original samples & 'fs' refers to the sampling frequency 
[y,fs] = wavread('handel.wav'); 
ydft = fft(y); %fft to transform the original signal into frequency domain 
n = length (y); %length of the original signal 
%  y has even length 
ydft = ydft(1:length(y)/2+1); 
% create a frequency vector 
freq = 0:fs/length(y):fs/2; 
shiftfreq = fftshift(freq); 

%plot original signal in time domain; 
figure; 
plot ((1:n)/fs, y); 
title('handel.wav in time domain'); 
xlabel ('second'); 
grid on; 

% plot magnitude in frequency domain 
figure; 
plot(freq,abs(ydft)); 
title('handel.wav in frequency domain'); 
xlabel ('Hz'); 
ylabel('Magnitude'); 
grid on; 

% plot phase in frequency domain 
figure; 
plot(freq,unwrap(angle(ydft))); 
title ('handel.wav in frequency domain'); 
xlabel ('Hz'); 
ylabel ('Phase'); 
grid on; 

回答1:


What you are currently doing now is plotting the half spectrum, so from 0 <= f < fs/2 where fs is the sampling frequency of your signal, and so fs/2 is the Nyquist frequency. Take note that considering the half spectrum is only valid if the signal is real. This means that the negative spectra is symmetric to the positive spectra and so you don't really need to consider the negative spectra here.

However, you would like to plot the full spectrum of the magnitude and phase. Take note that when calculating the fft using MATLAB, it uses the Cooley-Tukey algorithm so when computing the N point FFT, half of result is for the frequencies from 0 Hz inclusive up to fs/2 Hz exclusive and the other half is for the frequencies from -fs/2 Hz inclusive up to 0 Hz exclusive.

As such, to plot the full spectrum, simply perform a fftshift on the full signal so that the right half and left half of the spectrum is swapped so that the 0 Hz frequency is located in the centre of the signal. Also, you must generate frequencies between -fs/2 to fs/2 to cover the full spectrum. Specifically, you need to generate N points linearly spaced between -fs/2 to fs/2. However, take note that the Nyquist frequency at fs/2 Hz is being excluded at the end, so you need to generate N+1 points between -fs/2 to fs/2 and remove the last point in order for the right step size between each frequency bin to be correct. The easiest way to generate this linear array of points is by using the linspace command where the start frequency is -fs/2, the ending frequency is fs/2 and you want N+1 points between this range and remove the last point:

freq = linspace(-fs/2, fs/2, n+1);
freq(end) = [];

As such, borrowing some parts of your code, this is what the modified code looks like to plot the full spectrum of the magnitude and phase:

%// Read in sound file
[y,fs] = wavread('handel.wav'); 

%// Take N-point FFT where N is the length of the signal
ydft = fft(y);
n = numel(y); %// Get N - length of signal

%// Create frequency vector - make sure you remove last point
freq = linspace(-fs/2, fs/2, n+1);
freq(end) = [];

%// Shift the spectrum
shiftSpectrum = fftshift(ydft);

%//plot original signal in time domain; 
figure; 
plot ((0:n-1)/fs, y); %// Note you should start from time = 0, not time = 1/fs
title('handel.wav in time domain'); 
xlabel ('second'); 
grid on; 

%// plot magnitude in frequency domain 
figure; 
plot(freq,abs(shiftSpectrum)); 
title('handel.wav in frequency domain'); 
xlabel ('Hz'); 
ylabel('Magnitude'); 
grid on; 

%// plot phase in frequency domain 
figure; 
plot(freq,unwrap(angle(shiftSpectrum))); 
title('handel.wav in frequency domain'); 
xlabel('Hz'); 
ylabel('Phase'); 
grid on; 

I don't have access to your handel.wav file, but I'll be using the one provided with MATLAB. You can load this in with load handel;. The sampling frequency is stored in a variable called Fs, so I had to do fs = Fs; before the code I wrote above could work. The sampling frequency for this particular file is 8192 Hz, and this is approximately a 9 second long file (numel(y) / fs = 8.9249 seconds). With that file, this is the magnitude and phase that I get:




回答2:


For the discrete Fourier transform (DFT) as well as its fast implementations (FFTs), the frequencies are normalized with the sampling frequency fs, i.e., the original range -fs/2:fs/2 is changed to -pi:pi.

Besides, the DFT/FFT always starts with 0, and you can use fftshift() to shift the 0 frequency to the center. Therefore, after fftshift(), the range is -pi:pi, then, you can scale to -fs/2:fs/2.




回答3:


look at the following Matlab function, it can calculate phase spectrum as well as amplitude spectrum with a perfect accuracy:

https://www.mathworks.com/matlabcentral/fileexchange/63965-amplitude-and-phase-spectra-of-a-signal--fourier-transform-

This program calculates amplitude and phase spectra of an input signal with acceptable accuracy especially in the calculation of phase spectrum.The code does three main jobs for calculation amplitude and phase spectra. First of all, it extends the input signal to infinity; because for calculation Fourier transform(FT) (fft function in Matlab), we consider our signal is periodic with an infinite wavelength, the code creates a super_signal by putting original signal next to itself until the length of super_signal is around 1000000 samples, why did I choose 1000000 samples? Actually, it is just based on try and error!! For most signals that I have tried, a supper signal with 1000000 samples has the best output.

Second, for calculating fft in Matlab you can choose different resolutions, the Mathwork document and help use NFFT=2^nextpow2(length(signal)), it definitely isn't enough for one that wants high accuracy output. Here, I choose the resolution of NFFT=100000 that works for most signals.

Third, the code filters result of FT by thresholding, it is very important step! For calculating phase spectrum, its result is very noisy because of floating rounding off error, it causes during calculation "arctan" even small rounding off error produces significant noise in the result of phase spectrum, for suppressing this kind of noise you can define a threshold value. It means if amplitude of specific frequency is less than predefined threshold value (you must define it) it put zero instead of it.

These three steps help to improve the result of amplitude and phase spectra significantly.

IF YOU USE THIS PROGRAM IN YOUR RESEARCH, PLEASE CITE THE FOLLOWING PAPER:

Afshin Aghayan, Priyank Jaiswal, and Hamid Reza Siahkoohi (2016). "Seismic denoising using the redundant lifting scheme." GEOPHYSICS, 81(3), V249-V260. https://doi.org/10.1190/geo2015-0601.1



来源:https://stackoverflow.com/questions/33330627/plotting-the-magnitude-and-phase-spectra-of-a-wav-file-in-the-range-of-fs-2-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!