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 t equals 0 comma 1 comma ellipsis comma upper N minus 1 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 f equals 400 and the time points are evenly spaced values in left-bracket 0 comma left-parenthesis upper N minus 1 right-parenthesis slash f right-bracket.

The following optional arguments are supported:

WINDOW= Window

specifies the windowing function for the STFT. If Window is a positive integer, L, then a Hanning window of length L is used. If Window is a vector that contains L values, such as is produced by the TFWINDOW function, then the values specify the shape of the windowing function.

The relative length of the windowing function determines the resolution of the STFT. A relatively wide window provides good resolution in the frequency domain at the expense of poorer resolution in the time domain. In contrast, a narrow window provides good resolution in the time domain but poorer resolution in the frequency domain.

By default, L is the lesser of N and 256. See also the window argument for the TFSTFT function.

OVERLAP= R

specifies the amount of overlap between consecutive windows. This value must be less than L (which is specified by the WINDOW= argument).

In practice, many analysts use an overlap value that is 50% of the window length (Harris 1978; Trethewey 2000). Heinzel, Rüdiger, and Schilling (2002) provide a table of recommended overlaps for many common window shapes. The recommended overlap for many nonrectangular windows is between 50% and 75%.

By default, upper R equals left floor upper L slash 2 right floor. See also the overlap argument for the TFSTFT function.

FFTLEN= K

specifies a positive integer to be used to calculate the K-point fast Fourier transform (FFT) of the windowed time series data. By default, K is chosen to be the greater of L and 256. See also the fftlen argument for the TFSTFT function.

TITLE= Title

specifies a character string to use as the title for the spectrogram plot. By default, the global TITLE1 value is used. If the TITLE1 value is null, then the spectrogram does not include a title.

COLORRAMP= Colorramp

specifies a color ramp that assigns colors to cells in the spectrogram. For more information about how to specify a color ramp, see the HEATMAPCONT call. By default, the color ramp is the "TwoColorRamp" in the current ODS style.

SAMPRATE= f

specifies the sampling frequency in samples per second, where f must be positive. By default, f=1 Hz.

TRUNCATE= alpha

specifies a truncation value, which must be in the interval left-bracket 0 comma 1 right-parenthesis. If a is the minimum value in the spectrogram and b is the maximum value, then values that are less than a plus reverse-solidus Argument left-brace alpha right-brace asterisk left-parenthesis b minus a right-parenthesis are truncated to a. This can be useful when the energy is a signal span many orders of magnitude. By default, alpha=0.

SCALE= 'LINEAR' | 'LOG' | 'DB'

specifies whether the spectrogram plots the power, the logarithm of the power, or the power in decibels. You can specify the following values:

'LINEAR'

requests that the spectrogram display the power, which is the squared magnitude of the STFT.

'LOG'

requests that the spectrogram display log Subscript 10 Baseline left-parenthesis upper P right-parenthesis, where P is the power.

'DB'

requests that the spectrogram display the power in decibels, which is the quantity 10 log Subscript 10 Baseline left-parenthesis upper P right-parenthesis, where P is the power.

By default, SCALE='LINEAR'.

PANEL= 0 | 1

specifies whether to plot the spectrogram by itself or as part of a panel of graphs. You can specify the following values:

0

displays only the spectrogram and a color ramp.

1

displays three graphs in a panel:

  • The spectrogram, which has axes for frequency and time, and a color ramp.

  • Above the spectrogram is a plot of the original signal. The time axis of the signal is parallel to the time axis of the spectrogram.

  • To the left of the spectrogram is a plot of the average power in each frequency bin.

By default, PANEL=0.

The relationship between the arguments to the spectrogram must satisfy R less-than upper L less-than-or-equal-to K and upper L less-than-or-equal-to upper N, 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

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 t equals 1 comma 2 comma 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 t equals 1 comma 2 comma 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

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 f left-parenthesis t right-parenthesis equals sine left-parenthesis 120 pi t squared right-parenthesis 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

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 10 squared equals 100 whereas the blue cells indicate a power of approximately 10 Superscript negative 6. You can see that the power increases linearly in time, from zero frequency at t equals 0 to a frequency of 360 Hz at t equals 3.

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, where n is 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. The WORK._STFT data 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 left-parenthesis n comma x right-parenthesis.

    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 Power variable.

    dBPower

    The logPower variable 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, or dBPower.

    uFreq

    The unique values of the Freq variable. 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 Frequency variable. These values are represented by the colors in the leftmost heat map in a spectrogram panel.

    x0 and x1

    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;
Last updated: May 07, 2026