在下面的代码中我试图得到spectrogram
的非平稳信号x
在运行代码后,我希望看到一些像发布的内容" image_2" ,频率与时间的表示。但已发布代码的重新开头为image_1
。
任何人都可以指导我获取正确的spectrogram
吗?
代码
% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 1; % seconds
t = (0:dt:StopTime-dt); % seconds
t1 = (0:dt:.25);
t2 = (.25:dt:.50);
t3 = (.5:dt:.75);
t4 = (.75:dt:1);
%get a full-length example of each signal component
x1 = (10)*sin(2*pi*100*t);
x2 = (10)*sin(2*pi*200*t);
x3 = (10)*sin(2*pi*300*t);
x4 = (10)*sin(2*pi*400*t);
%construct a composite signal
x = zeros(size(t));
I = find((t >= t1(1)) & (t <= t1(end)));
x(I) = x1(I);
I = find((t >= t2(1)) & (t <= t2(end)));
x(I) = x2(I);
I = find((t >= t3(1)) & (t <= t3(end)));
x(I) = x3(I);
I = find((t >= t4(1)) & (t <= t4(end)));
x(I) = x4(I);
NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y
Y = fft(x, NFFT);
f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);
figure;
plot(f(1:200), 2 * abs( Y( 1:200) ) );
T = 0:.001:1;
spectrogram(x,10,9);
ylabel('Frequency');
axis(get(gcf,'children'), [0, 1, 1, 100]);
发布代码的结果:Spectrogram_Image_1 :
我想要获得的内容:Image_2:
Update_1,图片 的代码:
%now call the spectrogram
spectrogram(x, window, noverlap, Nfft, Fs);
ylabel('Frequency');
axis(get(gcf,'children'), [0, 1]);
答案 0 :(得分:1)
首先,与您第一次提出这个问题一样,您是否在时域(即plot(t, x)
)中绘制了数据并放大了转换以确保您的信号符合您的想法它是?它是否有四个不同的时期具有不同的频率?
假设确实如此,我很确定您的spectrogram
电话没有按照您的意愿行事。我认为你只获得10的NFFT,这意味着你的箱子宽800 Hz,这不足以解决你的频率只相差100 Hz。
在我看来,你应该指定更多参数,以便你知道它在做什么。您可以指定一个能够提供所需频率分辨率的Nfft。分辨率高于100赫兹(让我们尝试25赫兹)的东西,但不需要这么多的点,它比你有稳定频率的持续时间长(所以,小于0.25秒,这意味着小于2000点)。
为了了解如何指定FFT的长度,我查看了文档:http://www.mathworks.com/help/signal/ref/spectrogram.html
根据文档我会尝试五个参数版本:spectrogram(x,window,noverlap,nfft,fs)
对于您的代码,Fs
和x
正如您已经定义的那样,频谱图调用看起来像:
%define FFT parameters
des_df_Hz = 25; %desired frequency resolution for the display, Hz
Nfft = round(FS / des_df_Hz); %general rule for FFT resolution
Nfft = 2*Nfft; %double the bins to account for spreading due to windowing
Nfft = 2*round(0.5*Nfft); %make Nfft an even number
window = Nfft; %make your window the same length as your FFT
noverlap = round(0.95); %overlap a lot to make the plot pretty
%now call the spectrogram
spectrogram(x, window, noverlap, Nfft, Fs,'yaxis');