# STFT in Matlab

The following matlab segment, adapted from §G.3, illustrates the above processing steps:
Xtwz = zeros(N,nframes); % pre-allocate STFT output array M = length(w); % M = window length, N = FFT length zp = zeros(N-M,1); % zero padding (to be inserted) xoff = 0; % current offset in input signal x for m=1:nframes xt = x(xoff+1:xoff+M); % extract frame of input data xtw = w .* xt; % apply window to current frame xtwz = [xtw(Mo2+1:M); zp; xtw(1:Mo2)]; % windowed, zero padded Xtwz(:,m) = fft(xtwz); % STFT for frame m xoff = xoff + R; % advance in-pointer by hop-size R end

Time-Frequency Analysis

Introduction
The purpose of this project is to code and experiment with four of the primary time-frequency analysis techniques. The four techniques are the short time Fourier transform (STFT.m), the discrete wavelet (Haar) transform (DWT2.m), the continuous wavelet (Morlet) transform (CWVT.m), and the pseudo-Wigner distribution (PWD.m). Each of these transforms were coded in MATLAB and tested on a chirp signal (i.e., an increasing frequency as time increases). After successfully implementing the transforms, the code was used to investigate a real world signal. In our case, the closing price of Micron Technology stock over a decade was used.

Real World Data
The analysis data (mu.m) is the closing stock price for Micron Technology. It has long been held that the DRAM memory business has a cyclic revenue stream. Several companies produce DRAMs, but this is usually only one of many sectors within any specific company. Financial figures for any one sector of a company are not generally available. Micron (ticker symbol: MU) has both memory and personal computer sales, but their foundation rests on the semiconductor side. This makes their stock the best indicator of the memory business outside of proprietary financial data. The data is a bi-monthly sampling of the closing price roughly over a ten year period (256 sample points). The file (mu.m) contains the split adjusted price of the stock. Comments in the file show the date of the closing price and the split adjustment. The file (mu1.m) contains the same data as mu.m, but is not split adjusted. The last file, (mu2.m), is the same data as mu1.m,

but contains a dollar cost adjustment equal to a 3% annual percentage rate compounded monthly (0.25% / month). Since we are interested in the cycle of the stock, and not the growth factor, mu2.m data was used. Furthermore, the log (base exp) was applied to flatten the data, and finally, the mean was subtracted. This gives us a zero mean signal with a dollar cost adjustment to the closing price of the stock. The original mu.m and adjusted signal (log(mu2.m)-mean(mu2.m)) are shown at the top of Fig. 2.

Short Time Fourier Transform
The short time Fourier transform (STFT) function is simply the Fourier transform operating on a small section of the data. After the transform is complete on one section of the data, the next selection is transformed, and the output "stacked" next to the previous transform output. This process allows the type of window, window length, and zero padding within the FFT to be adjusted. The window type for our code include rectangular, Hamming, Hanning, and BlackmanTukey. The results from the chirp signal are shown in Fig. 1. The real world data results are shown in Fig. 2. Only our "best" results are displayed in Fig. 2.

m . Short time Fourier transform of chirp.Figure 1.

peaks at 130.7 years. 2. The lower frequency is approximately equal to a period of 4. Just by looking at the time based data in the second from the top frame. then goes negative again. which corresponds with our results.6 years. one can envision a sinusoid that goes negative at time zero. This implies a cycle of about 5 years. two markers have been added to indicate strong frequency peaks. Overall. the data appears to have several harmonic frequencies that are time varying. The higher frequency marker corresponds to approximately 1. but has substantially less energy. .As seen in the last frame of Fig.

Figure 2. Short time Fourier transform of the real world data .

Discrete Wavelet (Haar) Transform The discrete wavelet Haar transform uses a different basis than the STFT. The results from the chirp signal analysis are shown in Fig. The STFT uses complex sinusoids. maintain the same number of oscillations within the window. We used the most basic: the Haar wavelet. 3 with varying degrees of resolution in frequency (~ scales). on the other hand. It has other properties such as the function must always be bandpass. which are shaped by the window function. with increasing frequency. Wavelet transforms. but compress or dilate the window to adjust the frequency. . A wide variety of basis functions are available.

Figure 3.m . Discrete wavelet (Haar) transform of chirp.

One interesting observation. but not with the same window length. which shows the value of different analysis techniques. 4 shows several spikes at 0.02 which is in between the markers in the STFT results. is the high spike across multiple frequencies in Fig.The results from the DWT support the STFT data. 4 at approximately 140 (70 months). Similar results can be show with the STFT. This is due to the large downward step taken by the stock at that point. The final frame in Fig. both the smooth and spiky trends in the data. . but more coarsely. The DWT captured in one plot.

The big difference is that the windowing function shifts frequency by scaling rather than modulating . The wavelet function is analogous to the windowing function of the STFT and scale is analogous to frequency. Discrete wavelet (Haar) transform the real world data Continuous Wavelet Transform The Continuous Wavelet Transform (CWT) has much in common with the Short Time Fourier Transform (STFT). The wavelet function must be square integrable and be bandpass (have no energy at zero frequency). which holds for the above properties and has the following form: where sigma is the approximate bandwidth and fo is the center frequency. and s is the scale variable (dimensionless). The equation for the CWT is as follows: where x is the signal. w is the wavelet function.Figure 4. We used a real Morlet Wavelet. . along with the corresponding frequency domain information. The following figure shows examples of the Morlet wavelet function at three different scales.

Finally.Figure 5. The following picture depicts our implementation of the CWT over four scales using chirp. This is not the case for the STFT.m as our input signal. we implement the CWT by sampling at t = n/s. where the frequency resolution is the same over all frequencies. Accordingly. . so it will decrease at higher frequencies and increase at lower frequencies. Morlet wavelet at different scales Note that frequency resolution is proportional to bandwidth. we get higher time resolution at higher frequencies and lower time resolution at lower frequencies.

m" was used as our signal. "mu2. This corresponds to a what we might see with a Fourier Transform of a chirp. as we would expect. Continuous wavelet transform of a linear chirp signal Note that at lower scales we have better time resolution. We also see the linear time versus frequency characteristic of a chirp signal. We can also see the chirp has a more even frequency spectrum as our time resolution gets worse (frequency improves). which has perfect frequency resolution and no time resolution (so we just see an even spread of energy at most frequencies). The following figure shows the CWT of this input data. Next we tested the CWT on real world data.Figure 6. . with the adjustments to this signal explained in the "Real World Data" section above.

Pseudo-Wigner Distribution The MATLAB function PWD. 8. just like our data. This tells us that we have higher frequency components at these times. We can see brighter peaks around t = 160 and t = 250.m and the result is shown in Fig. . We can see that as our scale gets bigger (better frequency resolution).m calculates the pseudo-Wigner distribution. This corresponds to poor frequency resolution and the limit amount of high frequency components in the data. The peaks at low t correspond to the wrapping we did to periodize our data. Continuous wavelet transform of real world data We see that at the lowest scale we have a pretty smooth spectrum. we have some localization of energy carrying over scales. The function was tested with the chirp signal in chirp.Figure 7.

The Wigner distribution gives the best time-frequency resolution. Pseudo-Wigner distribution of a linear chirp signal As expected.007 (approximately a period of 5. The distribution shows a low frequency component at a normalized frequency of around 0.9 years) for most of the time duration. Most of the variations are slow (low frequency) with period greater than a year. . We obtained the Wigner distribution of the real world data "mu2. The result is shown in Fig.m" to analyze its timefrequency characteristics. 9. the time-frequency representation clearly shows a linearly increasing frequency characteristic with increasing time.Figure 8. This is expected from the earlier results using the STFT.

the fundamental frequency is in a range between 4. all the techniques gave some information.9 years. The implications are. that this predicts the cycle peak and not any given daily peak. Basically. . From the data. From the STFT and pseudo-Wigner distribution. A best guess puts the next cycle peak in the data at approximately March-May.6 to 5. the cyclic nature assures a predictable gain.9 to 3 years (half the range of the cycle period) to maximize their money. We wish all investors "happy hunting" if they want to use this data and conclusions to forecast Micron stock. the daily peaks can rise dramatically above the moving average. Invest today at (\$39. but specific information about this particular real world signal was best obtained through the pseudo-Wigner distribution and STFT. but at the same time. 2000. that an investor should expect to wait between 0.Figure 9.5 / share on 5/11/99) and check the price in a year! Realize. if history repeats itself. however. A secondary harmonic appears in the STFT at 1.7 years. Pseudo-Wigner distribution of the real world data Conclusions We see from all of the analysis techniques that strong low frequency components exist.

if any.) do not clearly indicate how the frequency content of a signal changes over time. Several parameters must be chosen: y y y y Block length. Note: Your browser doesn't currently support MathML. 1. each block is multiplied by a window that is tapered at its endpoints. Note: To see how the frequency content of a signal changes over time. If you are using Microsoft Internet Explorer 6 or above. DFT. we can cut the signal into blocks and compute the spectrum of each block. Any browser can view the math in the Print (PDF) version. Amount of overlap between blocks.Short Time Fourier Transform Module by: Ivan Selesnick Summary: Introduction to the Short Time Fourier Transform. That information is hidden in the phase .it is not revealed by the plot of the magnitude of the spectrum. To improve the result. STFT: Overlap Parameter . though they require an additional mathematics fonts package. DTFT. The type of window. which includes it's definition and methods for its use. R. blocks are overlapping 2. (Figure 1) Amount of zero padding. Firefox and other Mozilla browsers will display math without plugins. please install the required MathPlayer plugin. etc. Short Time Fourier Transform The Fourier transforms (FT.

STFT: Overlap Parameter Figure 1 The short-time Fourier transform is defined as X( .DTFT(x(n m) w(n) ) ) = .m) = (STFT(x(n) ) . .

3. The STFT of a signal x(n) is a function of two variables: time and frequency. is called the spectrogram of the signal.0 k N 1: in N equally spaced samples ¨ © ª k= . 4. The block length is determined by the support of the window function w(n) . (long window function). The STFT of a signal is invertible. 5. One can choose the block length.n= (x(n m) w(n) R 1 ( n) ) = n=0 (x(n m) w(n) (1) where w(n) is the window function of length R. 7. we sample the frequency axis from =0 to =2 . It is often used in speech processing. A wide-band spectrogram is one computed using a relatively short block length R. ( n) ) Sampled STFT To numerically evaluate the STFT.m) | . A long block length will provide higher frequency resolution (because the main-lobe of the window function will be narrow). A narrow-band spectrogram is one computed using a relatively long block length R. k. A short block length will provide higher time resolution because less averaging across samples is performed for each STFT value. A graphical display of the magnitude of the STFT. 6. (short window function). 1. 2. |X( .

(Xd(k. 0) (3) where 0.m) ) ) = n=0 (x(n m) w(n) WN (kn) ) = DFTN(x(n m) w(n) |n=0R 1. 0 is N R. the overlap between adjacent blocks is Rí1.0. The signal is shifted along the window one sample at a time. That generates more points than is usually needed. In this definition.2 N k ¸ ¹ º (2) We then have the discrete STFT. and the block length is Overlap=RíL Exercise 1 Match each signal to its spectrogram in Figure 2. the number of overlapping samples. . That means we usually evaluate Xd(k.X( 2 R 1 = N n=0 (x(n m) w(n) R 1 ( n) k.Lm) where L is the time-skip. The relation between the time-skip. so we also sample the STFT along the time direction.m) . .

Subfigure 2.1 Subfigure 2.2 .

Figure 2 [ Click for Solution 1 ] Spectrogram Example Figure 3 .

f.fs. % % % % % R: block length window function of length R N: frequency discretization L: time lapse between blocks fs: sampling frequency % COMPUTE SPECTROGRAM [B.overlap). % LOAD DATA load mtlb. overlap = R .x) xlabel('n') ylabel('x(n)') % SET PARAMETERS R = 256.L. clf imagesc(t. x = mtlb.log10(abs(B))). clf plot(0:4000. fs = 7418. L = 35.window.N. window = hamming(R).t] = specgram(x. N = 512.Figure 4 The matlab program for producing the figures above (Figure 3 and Figure 4). % MAKE PLOT figure(2). colormap('jet') . figure(1).f.

R = 256') Effect of window length R Narrow-band spectrogram: better frequency resolution Figure 5 Wide-band spectrogram: better time resolution .axis xy xlabel('time') ylabel('frequency') title('SPECTROGRAM.

1 Subfigure 7.Figure 5. Effect of Window Length R Subfigure 7.2 . Figure 6.Narrow-band spectrogram: better frequency resolution Figure 6 Here is another example to illustrate the frequency/time resolution trade-off (See figures . and Figure 7).

1 .) In each case. N = FFT length (Each block is zero-padded to length N.10} N {32.256} y y L = time lapse between blocks.Effect of Window Length R Figure 7 Effect of L and N A spectrogram is computed with different parameters: L {1. Exercise 2 For each of the four spectrograms in Figure 8 can you tell what L and N are? Subfigure 8. the block length is 30 samples.

They only affect the 'pixelation'.Subfigure 8.2 Figure 8 [ Click for Solution 2 ] L and N do not effect the time resolution or the frequency resolution. .

the data is in the file: stft_data.m. Each spectrogram is computed using a different set of parameters. match the above values of L and R.250} where y y R = block length L = time lapse between blocks. R {120.256.Effect of R and L Shown below are four spectrograms of the same signal.1024} L {35. you may listen to this signal with the soundsc command. . Figure 9 [ Click for Solution 3 ] If you like. Here is a figure of the signal. Exercise 3 For each of the four spectrograms in Figure 9.

Figure 10 .