Detailed Instructions (Recommended Steps in Python)
- Download the data file
signal.dat. This is a plain text file,
so you can load it into python using
for line in f
, ornumpy.loadtxt
, or a variety of other methods. Load it into python. Zero-pad the resulting array until it has a length of 1024 samples; for example, if you have it in a list, you could do
while len(signal) < 1024:
Use
signal.append(0)
complexspec=scipy.fftpack.fft(signal)
,magspec=abs(complexspec))
,phasespec=[cmath.phase(x) for x in complexspec]
,levelspec=[20*math.log10(x) for x in magspec]
to get the magnitude, phase, and level spectra (notice: the level spectrum is just the magnitude spectrum expressed in dB). Useplt.subplot
andplt.stem
to create stem plots of the time-domain signal, and of its magnitude, phase, and level spectra on four different subplots. Plot the magnitude and phase corresponding to the non-negative frequencies only --- you will lose points, this time, if you include any of the negative frequencies (i.e., any frequencies for which omega is greater thanmath.pi
). Hint: figure out how many frequencies there are between 0 andmath.pi
; give this number a name such asNfreqs
. Create a vectorfreqvals=[ math.pi*n/Nfreqs for n in range(0,Nfreqs) ]
, and useplt.stem(freqvals,magspec[0:Nfreqs)
. Likewise forphasespec
andlevelspec.
-
Create a length N=64 triangular window (this means that
the window covers samples 0 through 63, so the midpoint of
the window is sample 31.5, and its halfwidth is M=31.5;
use the formula in the lecture slides, or the one in the
textbook). Multiply your signal by this window using
something like:
tri_win_sig = [ tri_win[m]*signal[m] for m in range(0,64) ]
while len(tri_win_sig) < 1024:
tri_win_sig.append(0)
- Repeat part (b) using a Hamming window, and a Hann window.
- The signal consists of five sinusoids of various frequencies and amplitudes --- some are very weak, some are very strong. Look at the four different sets of spectra you've created, for rectangular, triangular, Hamming and Hann windows. Identify the frequencies (in radians/sample) and levels (in dB) of all five sinusoids, write these ten numbers in a text file, and hand in the text file with your lab submission.
Deliverables (Required)
By 4/4/2017 23:59, upload to compass a zip file named MYNAME_LAB8.ZIP containing the following things:
- Four images (rectangular, triangular, Hamming and Hann windowed signals), each containing four subplots (signal, magnitude spectrum, phase spectrum, and level spectrum), with the abscissa labeled in radians/sample between 0 and pi.
- A text file specifying the frequencies (in radians/sample) and levels (in dB) of the five sinusoids in the signal.
- Your code.
Walkthrough
Here's the video walkthrough for lab 8.