You are on page 1of 27

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.

Figure 1. Short time Fourier transform of chirp.m

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

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

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

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

Figure 4. 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 big difference is that the windowing function shifts frequency by scaling rather than modulating . The equation for the CWT is as follows:

where x is the signal, w is the wavelet function, and s is the scale variable (dimensionless). The wavelet function is analogous to the windowing function of the STFT and scale is analogous to frequency. The wavelet function must be square integrable and be bandpass (have no energy at zero frequency). We used a real Morlet Wavelet, which holds for the above properties and has the following form:

where sigma is the approximate bandwidth and fo is the center frequency. The following figure shows examples of the Morlet wavelet function at three different scales, along with the corresponding frequency domain information.

Figure 5. Morlet wavelet at different scales

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

Figure 6. Continuous wavelet transform of a linear chirp signal

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

Figure 7. Continuous wavelet transform of real world data

We see that at the lowest scale we have a pretty smooth spectrum. This corresponds to poor frequency resolution and the limit amount of high frequency components in the data. We can see brighter peaks around t = 160 and t = 250, just like our data. The peaks at low t correspond to the wrapping we did to periodize our data. We can see that as our scale gets bigger (better frequency resolution), we have some localization of energy carrying over scales. This tells us that we have higher frequency components at these times.

Pseudo-Wigner Distribution
The MATLAB function PWD.m calculates the pseudo-Wigner distribution. The function was tested with the chirp signal in chirp.m and the result is shown in Fig. 8.

Figure 8. Pseudo-Wigner distribution of a linear chirp signal

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

Figure 9. Pseudo-Wigner distribution of the real world data

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

Short Time Fourier Transform


Module by: Ivan Selesnick Summary: Introduction to the Short Time Fourier Transform, which includes it's definition and methods for its use. Note: Your browser doesn't currently support MathML. If you are using Microsoft Internet Explorer 6 or above, please install the required MathPlayer plugin. Firefox and other Mozilla browsers will display math without plugins, though they require an additional mathematics fonts package. Any browser can view the math in the Print (PDF) version.

Short Time Fourier Transform


The Fourier transforms (FT, DTFT, DFT, etc.) do not clearly indicate how the frequency content of a signal changes over time. That information is hidden in the phase - it is not revealed by the plot of the magnitude of the spectrum.

Note:
To see how the frequency content of a signal changes over time, we can cut the signal into blocks and compute the spectrum of each block. To improve the result, 1. blocks are overlapping 2. each block is multiplied by a window that is tapered at its endpoints. Several parameters must be chosen:
y y y y

Block length, R. The type of window. Amount of overlap between blocks. (Figure 1) Amount of zero padding, if any. STFT: Overlap Parameter

STFT: Overlap Parameter

Figure 1

The short-time Fourier transform is defined as


X( ,m) = (STFT(x(n) ) , ,DTFT(x(n m) w(n) ) ) =

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. 1. The STFT of a signal x(n) is a function of two variables: time and frequency. 2. The block length is determined by the support of the window function w(n) . 3. A graphical display of the magnitude of the STFT, |X( ,m) | , is called the spectrogram of the signal. It is often used in speech processing. 4. The STFT of a signal is invertible. 5. One can choose the block length. A long block length will provide higher frequency resolution (because the main-lobe of the window function will be narrow). A short block length will provide higher time resolution because less averaging across samples is performed for each STFT value. 6. A narrow-band spectrogram is one computed using a relatively long block length R, (long window function). 7. A wide-band spectrogram is one computed using a relatively short block length R, (short window function).
( n)

Sampled STFT
To numerically evaluate the STFT, we sample the frequency axis from =0 to =2 .
k,0 k N 1:

in N equally spaced samples


k=

2 N


(2) We then have the discrete STFT, (Xd(k,m) , ,X(
2

R 1

=
N

n=0 (x(n m) w(n) R 1


( n)

k,m) )

= n=0 (x(n m) w(n) WN (kn) ) = DFTN(x(n m) w(n) |n=0R 1,0, 0) (3) where 0, 0 is N R.

In this definition, the overlap between adjacent blocks is R1. The signal is shifted along the window one sample at a time. That generates more points than is usually needed, so we also sample the STFT along the time direction. That means we usually evaluate Xd(k,Lm) where L is the time-skip. The relation between the time-skip, the number of overlapping samples, and the block length is Overlap=RL
Exercise 1

Match each signal to its spectrogram in Figure 2.

Subfigure 2.1

Subfigure 2.2

Figure 2 [ Click for Solution 1 ]

Spectrogram Example

Figure 3

Figure 4

The matlab program for producing the figures above (Figure 3 and Figure 4).

% LOAD DATA load mtlb; x = mtlb; figure(1), clf plot(0:4000,x) xlabel('n') ylabel('x(n)') % SET PARAMETERS R = 256; window = hamming(R); N = 512; L = 35; fs = 7418; overlap = R - L; % % % % % R: block length window function of length R N: frequency discretization L: time lapse between blocks fs: sampling frequency

% COMPUTE SPECTROGRAM [B,f,t] = specgram(x,N,fs,window,overlap); % MAKE PLOT figure(2), clf imagesc(t,f,log10(abs(B))); colormap('jet')

axis xy xlabel('time') ylabel('frequency') title('SPECTROGRAM, R = 256')

Effect of window length R


Narrow-band spectrogram: better frequency resolution

Figure 5 Wide-band spectrogram: better time resolution

Narrow-band spectrogram: better frequency resolution

Figure 6

Here is another example to illustrate the frequency/time resolution trade-off (See figures - Figure 5, Figure 6, and Figure 7).
Effect of Window Length R Subfigure 7.1

Subfigure 7.2

Effect of Window Length R

Figure 7

Effect of L and N
A spectrogram is computed with different parameters: L {1,10} N {32,256}
y y

L = time lapse between blocks. N = FFT length (Each block is zero-padded to length N.)

In each case, the block length is 30 samples. Exercise 2

For each of the four spectrograms in Figure 8 can you tell what L and N are?
Subfigure 8.1

Subfigure 8.2

Figure 8 [ Click for Solution 2 ]

L and N do not effect the time resolution or the frequency resolution. They only affect the 'pixelation'.

Effect of R and L
Shown below are four spectrograms of the same signal. Each spectrogram is computed using a different set of parameters. R {120,256,1024} L {35,250} where
y y

R = block length L = time lapse between blocks.

Exercise 3

For each of the four spectrograms in Figure 9, match the above values of L and R.

Figure 9 [ Click for Solution 3 ]

If you like, you may listen to this signal with the soundsc command; the data is in the file: stft_data.m. Here is a figure of the signal.

Figure 10

You might also like