MATLAB: Can I set a range for spectrogram Analysis

fftfreqzspectrogram

Hi All,
my signal data has a variable frequency(around 50HZ). Can I set a frequency range for spectrogram analysis?
I want to determine the exact frequency according to time
My code is listed below.
Thank you ALL!
Ivy
clear all;
clc;
Fs=1000;
T=1/Fs;
L=1000;
t=(0:L-1)*T;
for i=1:1:10000 %variable frquency signal.
x(i)=0.7*sin(2*pi*(50+rand)*T*(i-1));
end
NFFT =1024;
[S,F,T,P] = spectrogram(x,1024,1000,1024,1E3);
[c,index]=max(P);
surf(T,F,10*log10(P),'edgecolor','none'); axis tight;
view(0,90);

Best Answer

Hi Ivy,
There are several issues with the code that you have posted. Each of these issues falls into one of two broad categories:
  1. Issues with how the code generates the source signal x(t), and
  2. Issues with how the code attempts to analyze and visualize the signal using the spectrogram function.
The most important issue is related to category #2. The sampling rate Fs is 1000 samples per second, while the number of samples in each FFT is 1024 samples. As a result, the frequency resolution of the spectrogram is nearly 1 hertz:
dF = Fs/NFFT = 1000/1024 = 0.9766 hertz
The frequency of the source signal is 50+rand hertz. Since the rand function generates a uniformly distributed random value between 0 and 1, the frequency of the signal will vary between 50 and 51 hertz, which is a range of only 1 hertz. This range is only just slightly larger than the resolution of the spectrogram, so that most of the time the spectrogram will not be able to detect it.
So to address this issue, you can either increase NFFT to a larger value, or increase the range of frequencies in the source signal.
As I mentioned, there are other issues in the code, but the frequency resolution is probably the most important one.
HTH.
Rick