Log Booklet for EE2 Experiments Vasil Zlatanov DFT experiment Exercise 1 Code for sinegen.m function y = sinegen(fsamp, fsig, nsamp) tsamp = 1/fsamp; t = 0 : tsamp : (nsamp-1)*tsamp; y = sin(2*pi*fsig*t); plot (y); grid title ( Sine Wave ) xlabel ( Time ) ylabel ( Amplitude ) end Exercise 2 Simply ploting S does not give us the correct value because of the way matlab stores S. Plotting S plots the matrix in the wrong orientation. So instead of plotting 10 sine curves with 128 samples. We plot 128 curves with 10 samples each, which is not what we want to do. For that we will have to rotate the matrix S And now we plot f k (t) = 10 n=1 sin(nw s t k ) So this is the output when all the harmonics have the same amplitude. Now using the weighting for the harmonics, such that the higher harmonics have a smaller amplitude: 1
Figure 1: plot of S / time in milliseconds Figure 2: transpose of S /time in milliseconds 2
Figure 3: plot / time in milliseconds Figure 4: with bn / time in milliseconds 3
We can see that the coefficients have made it so that the haromnics add up to what is approximately a sine square wave. Every 15ms or so they all add up to produce a nearly vertical edge and then cancel out to produce horizontal bits. And this is how cosine version looks: Figure 5: cosine with a_n / time in milliseconds They both imitate a sawtooth function, but the cosine wave is shifted by 10 milliseconds. In order to actually get the sawtooth function we need to add up the odd sines to infinity, and according to the Gibb s effect it will converge. For example here are the sine waves from 1 to 99: Mind you we will never get a perfect square wave because there will always be a spike at the corner. Exercise 3 Fourier transform is zero except at 1 = -4i and at 7 = +4i So we have a spike at 1 -> 1khz and a spike at 7khz which is the reflected dirac from the negative axis. Evaluating the same cosine signal we get 4 instead of 4i plotspec.m function y = plotspec(x) X = fft(x); 4
Figure 6: sine waves to 99 5
Figure 7: adding up haromnics up to 99 Figure 8: function of x 6
amp = sqrt( X.* conj (X)); phase = atan2( imag(x), real(x)); figure bar(amp); grid title ( Amplitude Spectra ) ylabel ( Amplitude ) figure plot (phase); grid title ( Phase Spectra ) ylabel ( Phase in radians ) end So using this function we can then graph the impulses: For [ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] Figure 9: plot And for [ 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] So they both have an amplitude of 1 but a different phase graph. 7
Figure 10: plot Figure 11: plot 8
Figure 12: plot Exercise 4 Using Hanning windows Exercise 5 Now doing the plotsec for the unknown signal Taking 256-samples they have the same frequency spectrum as the whole function. Namely the first maximum is at 53 ( the second is a reflection of that in the negative axis that appears on the positive side, a characteristic of matlab). The sampling frequency is 1000Khz and we have 1048 samples. max(amp) tells us that the 53 element has the max frequency of with magnitude 387.6. That would mean that the frequency is 53/(1000/1048) = 55.544 So it looks like the main majority of the signals is at 50Hz (which is the same frequency as the electricity in Europe). Exercise 6 300 Hz is at: 300/(1000/256) = 77 9
Figure 13: plot 10
Figure 14: plot Figure 15: phase 11
Figure 16: amplitude By excluding everything all indexes in the fft above 77 and below 256-77 (the reflection) amplitude The disadvantage to filtering in the frequency domain is that we lose information for samples we can not recover. Exercise 7 Here we have an impulsew ith 8 1 s and 8 0 s: Now with only 4 ones: With 2 ones: With 1 ones and 15 zeros: Exercise 8: With [1 0 0 0]: Infinite sum of sine waves. The fft of 4 1 s: We can see how the higher frequencies have been cut as it is a low pass filter. Convolution between [1 1 1 1] and uknown gives us: 12
Figure 17: 8 ones Figure 18: 4 ones 13
Figure 19: 2 ones Figure 20: 1 ones 14
Figure 21: unfiltered Figure 22: filtered with low-pass 15
Figure 23: convoluton 16
We can see that the high frequencies have gones down. The humps are caused by the fact that the spectra of a rectangular function is a sinc function. Exercise 9 Using unit impulse of with 1 one and 15 zeros, the response of the filter is: The impulse response of: y[n] = 0.5x[n] + 0.5 y[n-1] is: Input: Response from the filter: Figure 24: unit impulse 17
Figure 25: impulse response This looks like a sinc function. In frequency: Now doing the same with the unknown: The response from the filter: And in the frequency domain: 18
Figure 26: filter frequency response Figure 27: response in time 19
Figure 28: butterworth filter on unknown in frequency 20