# Spectogram of a signal

I am working on a seismic signal to extract long period long duration earthquakes. The sampling frequency is $2004\ \rm{ Hz}$ and the the signal has $20755200\ \rm{samples}$. The spectogram seems to be very discrete and it is not smooth.

I need to work in windows in a range of $300\ \rm{sec}$, so when I zoom in, it is even more like blocks.

What I need to a smooth spectogram like this:

Here is the MATLAB code:

y=y-mean(y); % y is the input signal
fc = 8; % Cut off frequency
fs = 2004; % Sampling rate
[b,a] = butter(6,fc/(fs/2)); % Butterworth filter of order 6
x = filter(b,a,y); % Will be the filtered signal
[b,a] = butter(6,2/(fs/2),'high'); % high-pass filter
xx = filter(b,a,x);% high pass filter
xx   = sgolayfilt(xx, 6, 9); %smooth the data
specgram(xx,fs);
ylim([0 0.02]);
caxis([30 150]);


• You'll have to play the time-frequency resolution tradeoff game here. Try change the window length and overlap in your specgram() call. Also, it'll help if you plot absolute frequencies on the y-axis by passing the sampling rate to the function. Nov 17, 2017 at 15:16
• What is the significance of "fs = 2004;" in the code? Regard <a href="journals.telkomuniversity.ac.id/">Telkom University</a> Feb 22 at 13:27

You will need to understand what the spectogram function is doing in order to improve your results.

As an starting point you first need to differentiate between smoothness and resolution.

The frequency resolution and the time resolution depends on the size of your FFT (An spectogram or STFT is the concatenation of multiples FFTs):

frequency_resolution = fs/L = 2004/L
time_resolution = L/fs = L/2004


This is called the time-Frequency compromise since the time and frequency resolution will be inversely proportional.

Now, from what you call "a smooth spectogram" I see your frequency range of interest goes from 0 to 100 Hz and you have said you will be looking at windows of 300 seconds, so for instance, a FFT size, L, of 2048 samples would give you a frequency resolution of 0.97 Hz and a time resolution of 1.02 seconds.

From here, you can apply 2 different methods to smooth your spectogram WITHOUT increasing neither your frequency nor time resolution, zero-padding (interpolation) and overlapping.

As I was in a similar point some weeks ago I can recommend you the following free resources:

If this is the specgram() function you're using, you're passing it the sampling rate as the nfft argument when you're calling specgram(xx,fs);.

# specgram

Time-dependent frequency analysis (spectrogram).

### Syntax

B = specgram(a)
B = specgram(a,nfft)
[B,f] = specgram(a,nfft,Fs)
[B,f,t] = specgram(a,nfft,Fs)
B = specgram(a,nfft,Fs,window)
B = specgram(a,nfft,Fs,window,noverlap)
specgram(a)
B = specgram(a,f,Fs,window,noverlap)

That means that it performs the FFT on 2004 samples each block, or a duration of one second. It also assumes a default sampling rate of 2 Hz. Your function call should look something like this: specgram(xx,nfft,fs), where nfft is the size of the FFT block in samples. As Atul Ingle mentioned, you have to find a reasonable block size that suits your needs in terms of time and frequency resolution.

What you are seeing are the discrete bins over the small range of frequencies you are displaying, and the default shading used when plotting the frequency bins versus time. You have more pixels than bins and by default the frequency bins are stretched over the pixels.The default shading is FLAT, shading INTERP should eliminate the blockiness.

You could also increase the size of your DFT's and increase the overlap.

You could roll your own spectrogram and also consider zero padding each fft call, in addition to each of the previous suggestions.

specgram is also being depreciated:

>> help specgram

specgram

Spectrogram using a Short-Time Fourier Transform (STFT). specgram has been replaced by SPECTROGRAM. specgram still works but may be removed in the future. Use SPECTROGRAM instead. Type help SPECTROGRAM for details.

See also periodogram, spectrum.periodogram, pwelch, spectrum.welch, goertzel.


Your figure b) also has a higher frequency resolution, so increasing the DFT size is something you should at least do. Using shading INTERP doesn't increase resolution but it does often make the frequency features more distinct over several DFT frames.