2016-04-19 202 views
1

我想繪製一個特定的.wav聲音文件在-2000到+ 2000 Hz的頻率範圍內的功率譜圖。Matlab功率譜圖

嘗試:

這是我到目前爲止的代碼:

[s, Fs] = wavread('chord.wav'); 
Hs=spectrum.periodogram; 
psd(Hs,s,'Fs',Fs) 

我試着使用週期圖算法。但是所產生的plot範圍從0到20000Hz。那麼,我如何修改代碼,以便將其繪製在-2000到+ 2000 Hz之間呢?

任何幫助將不勝感激。

+0

可以在頻率爲負我的模型位移時程?如果您只是想修改軸範圍,請查看[here](http://www.mathworks.com/help/signal/ref/dspdata.psd.html) – shamalaia

+1

您可以將軸範圍修改爲A_C建議的值,或使用帶通濾波器。 – GameOfThrows

+0

什麼是'spectrum.periodogram'?什麼是'psd()'?我不認爲這些是標準的MATLAB函數/類? – Tom

回答

0

我修改的例子中的一個在此支承page

Fs = 32e3; 
    t = 0:1/Fs:2.96; 
    x = cos(2*pi*t*1.24e3)+ cos(2*pi*t*10e3)+ randn(size(t)); 
    nfft = 2^nextpow2(length(x)); 
    Pxx = abs(fft(x,nfft)).^2/length(x)/Fs; 
    Hpsd = dspdata.psd(Pxx(1:length(Pxx)/2),'Fs',Fs); 
    plot(Hpsd) 

enter image description here

figure 
plot(Hpsd) 
axis([9.8 10.2 -90 0]) %change axis range 

enter image description here

+0

非常感謝您的支持。代碼完美工作。儘管對於小於0的頻率,圖形完全是空白的,所以我不確定爲什麼問題是要求將_.wav_文件的功率譜從-2000到+ 2000 Hz繪製出來。 – Merin

0

我寫這個代碼採取任何時間歷史向量的FFT,其中T是時間步矢量和S是相關的位移或任何給定的數量,你希望繪製他們的光譜。 PS:STfile是所有自由度,我保存爲一個文件.MAT


clc 
clear 

load STfile 

% is your starting vector of time 

data = S(:,12); % vector of data you want to resample 

N = length(T); 

for ii=1:(N-1) 
    DT(ii) = T(ii+1)-T(ii); 
end 

DTS = mean(DT); 


data_TS = timeseries(data,T); % define the data as a timeseries 

new_T = T(1):DTS:T(end); % new vector of time with fixed dt=1/fs 

data_res = resample(data_TS,new_T); % data resampled at constant fs 

plot(data_res) 

y=getdatasamples(data_res,1:N); 

Fs=1./DTS; 
NFFT = 2^nextpow2(N); 
Y = fft(y,NFFT)/N; % the division by N is to scale the amplitude 

freq = Fs/2*linspace(0,1,NFFT/2+1); % vector of frequencies for the plot 
figure() 
plot(freq,2*abs(Y(1:NFFT/2+1)),'r') % multiplicated by 2 to recover the energy related 
% to the negative frequencies since in this way only half spectrum is plotted 
xlabel('Frequency(rad/sec)') 
xlim([0 0.05]) 

example result for this code(time history of heave velocity)