-->window('re',7)
ans =
! 1. 1. 1. 1. 1. 1. 1. !
-->window('tr',7)
ans =
! 0.25 0.5 0.75 1. 0.75 0.5 0.25 !
The command `window' generates a set of values with the same shape but different sizes. Its second parameter `7' indicates to `window' the length of the array that we are going to generate. The first one indicates the shape of the window. In the first command we indicated to `window' that we wanted a rectangular shape (`re'), in the second one (`tr') that we wanted a triangular shape.
Frequently we are interested in using windows with some special characteristics
-->window('hm',7)
ans =
! 0.08 0.31 0.77 1. 0.77 0.31 0.08 !
-->window('hn',7)
ans =
! 0. 0.25 0.75 1. 0.75 0.25 0. !
The command `window' with the option `hm' generates a hammning window and with the option `hn' a hanning window. Both are useful shapes in spectral analysis.
Now we are going to describe the influence of the window on the Fourier Transform. We begin with the example of the last chapter
-->N = 200;
-->inct = 0.01;
-->t = (0:N-1) * inct;
-->w = window('re',length(t));
-->frec = 9;
-->signal = 30 * sin (2 * %pi * frec * t + 0.4 * %pi) .* w;
-->incf = 1 / (N*inct);
-->f = (0:N-1) * incf;
-->ffsignal = fft (signal, -1);
In this example we used a signal of 200 points. It is a sinusoid at 9 Hz sampled at 0.01. We multiply the window by the signal. In this case, it is similar to let the signal unchanged and the result is
-->xsetech([0,0,1,0.5],[0,-40,2,30]) -->plot2d(t,signal) -->xsetech([0,0.5,1,0.5],[0,0,100,20]) -->plot2d(f,abs(ffsignal)/sum(w))And we get: We can see that from a sinusoid of an amplitude of 30 at a frequency of 9 Hz we get in the Fourier Transform two frequencies at 9 Hz and -9 Hz. Each of them has an amplitude of 15. Using a rectangular window is equivalent to using no window in the segment considered.
Now we are going to repeat the process with a hanning window
-->clear()
-->N = 200;
-->inct = 0.01;
-->t = (0:N-1) * inct;
-->w = window('hn',length(t));
-->frec = 9;
-->signal = 30 * sin (2 * %pi * frec * t + 0.4 * %pi) .* w;
-->incf = 1 / (N*inct);
-->f = (0:N-1) * incf;
-->ffsignal = fft (signal, -1);
The only difference with the previous example is that we use a hanning window. If we adjust the result of the Fourier Transform we get:
-->xsetech([0,0,1,0.5],[0,-40,2,30]) -->plot2d(t,signal) -->xsetech([0,0.5,1,0.5],[0,0,100,20]) -->plot2d(f,abs(ffsignal)/sum(w))Notice that the value of the module of the Fourier Transform is divided by the sum of the values of the window to increase the coincidence between amplitudes. The explanation will be more clear in the next chapter. A similar result would have been obtained with hamming or triangular window.
To see the advantages of `hamming' or `hanning' window, we are going to do a little modification in the frequency
-->xbasc()
-->N = 200;
-->inct = 0.01;
-->t = (0:N-1) * inct;
-->w = window('re',length(t));
-->frec = 9.3;
-->signal = 30 * sin (2 * %pi * frec * t + 0.4 * %pi) .* w;
-->incf = 1 / (N*inct);
-->f = (0:N-1) * incf;
-->ffsignal = fft (signal, -1);
And if we plot with the following code
-->xsetech([0,0,1,0.5]) -->plot2d(t,signal) -->xsetech([0,0.5,1,0.5]) -->plot2d(f,abs(ffsignal)/sum(w))we get: This figure shows a new phenomenon: `leakage'. The activity has `leaked out' and the peak is broader. Although we have only one frequency, the peak spans through several frequencies. This phenomenon can be minimized by using some windows. The next code shows the difference:
-->xbasc()
-->N = 200;
-->inct = 0.01;
-->t = (0:N-1) * inct;
-->w = window('hn',length(t));
-->frec = 9.3;
-->signal = 30 * sin (2 * %pi * frec * t + 0.4 * %pi) .* w;
-->incf = 1 / (N*inct);
-->f = (0:N-1) * incf;
-->ffsignal = fft (signal, -1);
-->xsetech([0,0,1,0.5])
-->plot2d(t,signal)
-->xsetech([0,0.5,1,0.5])
-->plot2d(f,abs(ffsignal)/sum(w))
and the result is:
Notice that we have improved the resolution of the peak by decreasing the leakage.
In the next chapter we are going to use `windowing' to estimate the frequency of the signal.