2016-04-19 199 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)