Signal processing

In this section, we are going to use NumPy functions to simulate several signal functions and translate them to Fourier transforms. We will focus on using numpy.fft and its related functions. We hope after this section that you will get some sense of using a Fourier transformation in NumPy. The theory part will be covered in the next section.

The first example we are going to use is our heartbeat signal, which is a series of sine waves. The frequency is 60 beats per minutes (1 Hz), and our sampling period is 5 seconds long, with a sampling interval of 0.005 seconds. Let's create the sine wave first:

In [1]: time = np.arange(0, 5, .005) 
In [2]: x = np.sin(2 * np.pi * 1 * time) 
In [3]: y = np.fft.fft(x) 
In [4]: show(x, y) 

In this example, we first created the sampling time interval and saved it to anndarray called time. And we passed the time array times 2π and its frequency 1 Hz to the numpy.sin() method to create the sine wave (x). Then applied the Fourier transform to x and saved it to y. Finally, we used our predefined method show() for visual comparison with the sine wave and its normalized Fourier transform, as you can see in the following graph:

Signal processing

The upper green line represents the heartbeat wave; since we use 1 Hz for 5 seconds, we obtained 5 continuous sine waves. One thing to note here is that our sampling interval is 0.005 seconds, which means we use 200 points (1/0.005) to simulate one wave, so it looks relatively smooth. If we increase our sampling interval (reducing the number of points for each wave), we will obtain a more intense sine wave. The lower part of the chart is the absolute value of the normalized Fourier transform based on the frequencies (the so-called spectrum). We can see that there is a high point at 1 Hz, which matches our original wave frequency.

Next, we are going to try computing multi-frequency sine waves to their Fourier transforms. After this, we may have a clearer picture of a Fourier transform. The following code shows you how to do this:

In [8]: x2 = np.sin(2 * np.pi * 20 * time) 
In [9]: x3 = np.sin(2 * np.pi * 60 * time) 
In [10]: x += x2 + x3 
In [11]: y = np.fft.fft(x) 
In [12]: show(x, y) 

First, we created two more sine waves with a different frequency, x2 with a frequency of 20 Hz, and x3 with 60 Hz, and we added them to the original 1Hz sine waves x. Then we passed the modified x to the Fourier transform and plotted the graph using the predefined show(). You can see this in the following graph:

Signal processing

In the upper green line chart, we can see that the sine waves are combined with different frequencies, but it is really difficult to distinguish them. However, after computing the Fourier transform, and converting them to the frequency domain, we can see clearly in the lower red line chart that three high points are identified, which are 1 Hz, 20 Hz, and 60 Hz. This matches up with our original sine wave's frequency.

From these two examples, you must be able to get some sense of the Fourier transform. Next we are going to demonstrate three more signal processings: one for square signals, one for pulses, and the other for random signals.

First we created square wave signals using numpy.zeros() with the same time intervals (time). We want the square wave frequency to be 10 Hz and the amplitude to be 1, so we set every 20th time interval (200 / 10) to be a value of one to simulate the wave and pass it to the Fourier transform, as you can see in the following code block:

In [13]: x = np.zeros(len(time)) 
In [14]: x[::20] = 1 
In [15]: y = np.fft.fft(x) 
In [16]: show(x, y) 

This code generates the following graph:

Signal processing

From the upper green line chart, we can see 50 continuous square waves in 5 seconds (10 Hz), but when we computed its Fourier transform we obtained several red high points in the spectrum instead of just one at 10 Hz. You may wonder whether square waves are also periodic functions, but why is the Fourier transform so different from the sine waves? Remember that the Fourier transform converts the time domain to the frequency domain but, underneath the hood, there are series of sine and cosine functions to decompose the original function. We can still see the red high points are regularly spaced, where the space is 10 Hz apart.

Next, we are going to generate a one-pulse signal, that doesn't have any frequency, and we are going to compute its Fourier transform. Compare this with the previous square waves, and you may have a better sense of the Fourier transform:

In [17]: x = np.zeros(len(time)) 
In [18]: x[380:400] = np.arange(0, 1, .05) 
In [19]: x[400:420] = np.arange(1, 0, -.05) 
In [20]: y = np.fft.fft(x) 
In [21]: show(x, y) 

First we created an all-zero ndarray the same size as the time variable, and then we generated the one-pulse signal, which occurred at 2 seconds (the 400th element of the x array). We occupied 40 elements around 2 seconds to simulate the pulse: 20 increasing from 0 to 1, and the other half decreasing from 1 back to 0. We passed the one-pulse signal to the Fourier transform and used show() for visual comparison.

The upper green line chart in the following graph is the one-pulse signal we simulated, and the lower red line chart is its Fourier transform. We can see the highest point in the lower chart occurred in frequency equal to 0, which makes perfect sense since our simulated signal didn't have any frequency. But, after zero-frequency, we can still see a couple of high points at different frequencies, which came from the transformation processes.

Signal processing

The last example in this section is random signal processing. As in the previous example, we also use 5 seconds as our total sampling period with 100 random signals, which doesn't have any fixed frequency associated with it. And we pass the random signals to the Fourier transform to obtain its frequency domain. The code block is as follows:

In [22]: x = np.random.random(100) 
In [23]: y = np.fft.fft(x) 
In [24]: show(x, y) 

The following is the graph generated by the code:

Signal processing

After going through these examples, we know how to use the Fourier transform in NumPy-simply call numpy.fft.fft()-and we gained some sense of what the Fourier transform looks like. In the next section, we will focus on the theory part.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208