Language Reference
SPECTROGRAM Call
CALL SPECTROGRAM (x) WINDOW=Window OVERLAP=R FFTLEN=K TITLE=Title COLORRAMP=ColorRamp SAMPRATE=f TRUNCATE=alpha SCALE='LINEAR' | 'LOG' | 'DB' PANEL=0 | 1 ;
This subroutine is supported only by the IML procedure.
The SPECTROGRAM subroutine displays a heat map that shows the power (squared magnitude) of the short-time Fourier transform (STFT) of the signal for various bins in the time and frequency domain. Internally, the STFT is computed by using the TFSTFT function. For more information about the STFT and spectrograms, see Rabiner and Schafer (2007).
The first four arguments of the SPECTROGRAM subroutine are the same as for the TFSTFT function. The first argument, x, is a required column vector of size N that contains the time series signal. The signal is assumed to be sampled at N evenly spaced time intervals. The remaining arguments are optional and can be specified in any order.
If the SAMPRATE= argument is not specified, the sampling frequency is assumed to be 1 Hz. So by default the time points are assumed to be seconds. If you specify a value f for the SAMPRATE= argument, then the signal is assumed to be sampled f times per unit of time. For example, if the signal is sampled at 400 Hz (that is, 400 times per second), then
and the time points are evenly spaced values in
.
The following optional arguments are supported:
The relationship between the arguments to the spectrogram must satisfy R K and
, where R is the overlap length, L is the window length, K is the FFT length, and N is the signal length.
The spectrogram is one way to visualize a signal in time and frequency domains simultaneously. The following DATA step constructs a signal that consists of different frequencies. The signal is sampled 100 times per second. For the first second, the frequency of the signal is 5 Hz. For the second second, the frequency of the signal is 10 Hz. The frequency is 20 Hz and 40 Hz for the third and fourth seconds, respectively.
%let SampRate=100;
data Signal;
keep t x;
pi = constant("pi");
/* sample at SampRate Hz */
do t = 0 to 4 by 1/&SampRate;
if t<1 then x = sin(2*pi*5*t);
else if t<2 then x = sin(2*pi*10*t);
else if t<3 then x = sin(2*pi*20*t);
else if t<4 then x = sin(2*pi*40*t);
output;
end;
run;
The power spectrum of the FFT of the signal shows peaks at frequencies 5, 10, 20, and 40. However, the FFT does not provide information when these frequencies occur. In contrast, a spectrogram indicates the frequencies in a signal and the time intervals during which those frequencies are dominant.
The following SAS/IML statements read the signal and create a spectrogram, which is shown in Figure 423. The SPECTROGRAM call specifies a window of length 50, which corresponds to 500 ms of this signal. By default, a Hanning window is used. Also by default, the overlap is 50% of the window size, which is a length of 25 time units.
proc iml;
use Signal; read all var {"t" "x"}; close;
winLen = (500 / 1000) * &SampRate; /* 500 ms = 50 time units */
call spectrogram(x, winLen) panel=1
sampRate=&SampRate colorramp="ThreeColor";
Figure 423: Spectrogram, 500 ms Window

The SPECTROGRAM call displays a panel of graphs. The panel contains four graphical components:
At the top of the panel is a plot of the time series. You can see that the signal consists of four sinusoidal curves and that the changes of frequency occur at
and 3 seconds.
Directly below the signal is the spectrogram, which is a heat map of the power in the time-frequency domain. The spectrogram shows four red horizontal line segments, which correspond to the maximum power of the signal. From the vertical axis, the maximum power appears to be located near the frequencies 5, 10, 20, and 40 on the vertical axis. The power appears to transition between frequencies near
and 3 seconds, but the spectrogram is blurry near the transition times. A shorter window would improve the resolution of the graph in time, but at the cost of worsening the resolution for frequency.
The legend on the far right shows the color ramp that encodes the power values. The range of the power appears to be in the interval [0, 150]. The ThreeColorRamp color ramp for the ODS style in this documentation is a blue-white-red blend.
The heat map on the far left indicates the average power for each frequency. The average is taken over time and indicates that the average power of this signal is greatest near the frequencies 5, 10, 20, and 40.
You can specify options that control the appearance of the spectrogram. The following statements specify a Blackman window that is 25 time units wide (250 ms). The overlap parameter is set to 80% of the window width, or 20 time units. The COLORRAMP= option calls the PALETTE function to obtain a yellow-orange-red color ramp. Only the spectrogram is shown because the PANEL=1 option is not specified. The spectrogram displays the title that was set by using the TITLE statement. The resulting spectrogram is shown in Figure 424.
title "Spectrogram of 250 ms window";
winLen = (250 / 1000) * &SampRate;
window = tfwindow(winLen, "Blackman");
call spectrogram(x, window, int(winLen*0.8))
sampRate=&SampRate colorramp=palette("YLORRD",7);
Figure 424: Spectrogram, 250 ms Window

The spectrogram in Figure 424 shows features that are similar to Figure 423, but Figure 424 provides a finer resolution in time because of the narrower window and the increased overlap parameter. This enables you to better estimate the times at which the signal changes frequencies.
The next example is a spectrogram for a linear chirp signal. A linear chirp is a sinusoidal wave whose frequency increases at a linear rate with time. The signal for the following example is for t in the interval [0, 3]. During that time period, the frequency of the signal increases at a linear rate from 0 to 360 Hz. The following statements define the signal and create a spectrogram, which is shown in Figure 425:
/* Linear chirp on interval [0,3] */
t = T( do(0, 3, 0.001) ); /* sampRate = 1000 Hz */
pi = constant('pi');
w = pi*120*t; /* frequency linear in time */
x = sin(w#t);
title "Linear chirp, 0-360 Hz in 3 secs";
winLen = 200;
window = tfwindow(winLen, "Gaussian");
noverlap = floor(0.6*winLen);
nfft = 256;
ramp = palette("Spectral",7)[7:1]; /* reverse "spectral" color ramp */
call spectrogram(x,window,noverlap,nfft) scale="log"
colorramp=ramp sampRate=1000;
Figure 425: Spectrogram of Linear Chirp

The spectrogram displays the power on a logarithmic (base 10) scale, so that the red cells indicate a power of approximately whereas the blue cells indicate a power of approximately
. You can see that the power increases linearly in time, from zero frequency at
to a frequency of 360 Hz at
.
A few remarks about the spectrogram:
If the time series contains N points, then an accurate graph of the signal requires at least N pixels in the horizontal direction. Because typical signals contains thousands of points, a plot of the time series might not be practical. Consequently, the SPECTROGRAM call displays only the spectrogram by default. For a short time series, you can specify the PANEL=1 option to display the time series above the spectrogram.
-
The NXYBINSMAX= option in the ODS GRAPHICS statement determines the maximum number of cells that are displayed in a heat map. By default, NXYBINSMAX=100,000. For long signals, the SPECTROGRAM call might produce an empty plot, in which case the SAS log displays the following:
The number of bins in HEATMAPPARM statement is x. It exceeds the NXYBINSMAX threshold in the ODS GRAPHICS statement. The plot will not be drawn.
To display the graph, submit the statement
ODS GRAPHICS / NXYBINSMAX=n, wherenis larger than the number of bins that is displayed in the log. -
The SPECTROGRAM subroutine creates the data set
WORK._STFT. The data set contains the time signal and the values from the short-time Fourier transform. TheWORK._STFTdata set contains the following variables:-
Signal The time series values x.
-
n The evenly spaced time points. The signal can be graphed by plotting the pairs
.
-
Power The power (squared magnitude) of the short-time Fourier transform (STFT) of the signal for each time-frequency bin in the spectrogram.
-
logPower The base-10 logarithm of the
Powervariable.-
dBPower The
logPowervariable multiplied by 10.-
Freq The frequency value of a cell in the spectrogram.
-
Time The time value of a cell in the spectrogram.
-
Z The response value for a bin in the spectrogram. This is equal to either
Power,logPower, ordBPower.-
uFreq The unique values of the
Freqvariable. These are the values of each row in the spectrogram. These values are the vertical positions of the cells in the leftmost heat map in a spectrogram panel such as Figure 423.-
EnergyPower The mean values of the rows of the spectrogram for each value of the
Frequencyvariable. These values are represented by the colors in the leftmost heat map in a spectrogram panel.-
x0andx1 Constants for the ODS template that defines the spectrogram panel. These values define the left and right edges of the leftmost heat map in a spectrogram panel.
-
You can use the WORK._STFT data to modify the spectrogram. For example, the following call to PROC SGPLOT uses the ThreeColorRamp color ramp to display a spectrogram of the most recently created signal. You can use the WORK._STFT data set to create additional graphs without recomputing the STFT.
title "Heat Map from Spectrogram Data";
proc sgplot data=_STFT;
heatmapparm x=Time y=Freq colorresponse=Z /
colormodel=ThreeColorRamp;
run;