Matlab 功率谱图
Matlab Power Spectrum Plot
我想绘制特定 .wav 声音文件的功率谱图,频率范围为 -2000 到 +2000 Hz。
尝试:
到目前为止,这是我的代码:
[s, Fs] = wavread('chord.wav');
Hs=spectrum.periodogram;
psd(Hs,s,'Fs',Fs)
我尝试使用周期图算法。但结果 plot 的范围是 0 到 20,000 Hz。那么,我该如何修改代码,使其在 -2000 到 +2000 Hz 范围内绘制?
如有任何帮助,我们将不胜感激。
我修改了这个支持中的一个例子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)
figure
plot(Hpsd)
axis([9.8 10.2 -90 0]) %change axis range
我编写此代码是为了对任何时间历史向量进行 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)
我想绘制特定 .wav 声音文件的功率谱图,频率范围为 -2000 到 +2000 Hz。
尝试:
到目前为止,这是我的代码:
[s, Fs] = wavread('chord.wav');
Hs=spectrum.periodogram;
psd(Hs,s,'Fs',Fs)
我尝试使用周期图算法。但结果 plot 的范围是 0 到 20,000 Hz。那么,我该如何修改代码,使其在 -2000 到 +2000 Hz 范围内绘制?
如有任何帮助,我们将不胜感激。
我修改了这个支持中的一个例子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)
figure
plot(Hpsd)
axis([9.8 10.2 -90 0]) %change axis range
我编写此代码是为了对任何时间历史向量进行 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)