Digital Signal Processing using MATLAB Part 2

arduino dsp

Click here to see Part 1 of this article>

arduino dsp

In the second part of our Arduino DSP series, we’ll continue delving into the fundamentals of digital signal processing. We’ll learn about characteristics of digital filters and how these can be applied when processing signals in MATLAB. In the following article, we’ll provide an in-depth tutorial of the Fourier Transform and examine the most important parameter of the voice signal: frequency.

 

Software

  • Matlab

 

Step 1: How to load the signal in Matlab

After you registered the voice signal using Audacity, now it’s time to process it in MATLAB. This functionality will be done with function wavread, which reads (.wav) sound files. Our output signal from Audacity has this extension. The input of this function is the name of the signal (testSound.wav), and in MATLAB function you only need to write “testSound”. The outputs for this function are as follows:

  • Y – the name of the signal
  • Fs – the sample rate of the signal
  • Bits – number of bits

This function can be realized using the following code below. The code also plays the sound for the user to hear how the signal transforms over time during the processing.

arduino dsp

Figure 1: The voice signal viewed in Matlab

If you want to acquire more information about your vocal signal, all you need to do is to type in the audiofile name in .wav, then the function audioinfo will return the sound parameters.

This function comes in very handy, especially when you have an unknown signal. For example, if you can’t find its sample rate, duration, and bit/sample, which are all essential information when processing a signal, you can use the above function to find more information about the particular signal.

arduino dsp

Figure 2: The properties of the signal

arduino dsp

Figure 3: The sound wave and its mean value

 

Step 2: Removing the mean value (or DC component)

The Direct Current (DC) component is the constant voltage added to a pure Alternating Current (AC) waveform (e.g. voice signal). The true average voltage for a pure AC waveform would be zero. A voice signal is analog, but when this was transformed into a digital signal with Audacity, it acquired a DC component. This happened because Audacity has its own range and it rescaled the signal. Usually, an analog signal has a value ranging from -0.5 to 0.5V. We need a 0.5 DC component because the program scales samples using a range of positive numbers (every sample will be transformed into a number from 0 to N; N is a natural number).

However, when you also have a DC component, the amplitude of the AC varies depending on the value of the component. Let’s take a look at Figure 4. Let’s assume that you have a sine wave with an amplitude of 2V peak-to-peak and you add a DC component that equals to 0.5 V. The amplitude of the final signal would be 1.5 V at the highest point and -0.5 V at the lowest points.     

We want to see if the signal really have a DC component and what its value is. We can do this by plotting the value or you can type in M in Command Window.

arduino dsp

Figure 5: The mean value of the signal

Removing the mean component is important because the voice signal doesn’t have a DC component and we want to keep it as a pure waveform. Inside the removeDC function, we have the following code which calculates the mean value and subtracts it from the original signal.

 

Step 3: Decimation process

Our sampling frequency is now 48000 Hz and we want to make it 16000 Hz. With decimation, we can reduce the sampling rate of the signal. This can be done using decimate function in MATLAB. When we are sampling a voice signal, the minimum frequency used should be 8 kHz, according to the Nyquist sampling theorem due to the maximum human hearing bandwidth (i.e. 4 kHz).

This operation was made because we want to see the differences between the initial signal and decimated sifnal and see how this modification may alter the initial signal.

arduino dsp

Figure 6: Decimated signal

When decimation (or downsampling) is made, you modified the sample rate at a lower rate compared to the previous value. As you can see in the picture below, the analog signal on the left side was sampled at a higher rate than the one on the right. This means that the sampling frequency was decimated.

We can also make upsampling, which is the opposite process of downsampling, and increase the sampling frequency. This can be done using interpolation technique and also a low pass filter.

 

Step 4: Filtering the signal

A digital filter is simply a discrete-time, discrete-amplitude convolver. Digital filters are important in signal processing because it can process multiple operations compared to an analog filter. (We can assume that the costs are higher for digital filters because we would need special digital signal processors which can operate the functions for a filtering algorithm.)

Every digital filter has different specification: passband, stopband, and ripple. Before we go any further, let’s review the following terms.

  • Passband: a band of frequencies that is allowed to pass through a filter.
  • Cut-off frequency: a certain frequency picked by the user which represents the theoretical frequency that can pass. This frequency must be lower than sampling frequency/2 in order to avoid the aliasing. In electronics, the cut-off frequency represents the value where the power of the signal becomes two times less.
  • Stopband: a band of frequencies where all the frequency higher than this band cannot pass.

Digital filter can be split in 2 categories. FIR (finite impulse response) can be used with the integer coefficients. IIR (infinite impulse response) has an analog equivalent and it is more efficient in such a way that it has an output with a lower filter order.

One of the most important properties of FIR/IIR filters is a phase characteristic:

  • FIR: linear phase characteristic
  • IIR: nonlinear phase characteristic (or phase distortion)

FIR Filter Properties – dspGuru by Iowegian International

“Linear Phase refers to the condition where the phase response of the filter is a linear (straight-line) function of frequency (excluding phase wraps at +/- 180 degrees). This results in the delay through the filter being the same at all frequencies. Therefore, the filter does not cause “phase distortion” or “delay distortion”. The lack of phase/delay distortion can be a critical advantage of FIR filters over IIR and analog filters in certain systems, for example, in digital data modems.”

We can choose from many types of digital filters in MATLAB, but we need to understand how to apply them to our specific application. We are going to use an IIR filter for this project because it is more efficient, i.e. we need a smaller order compared to the FIR filter.

arduino dsp

Figure 8: IIR filters

Add the following lines in MATLAB to create a Butterworth filter. This function returns filter coefficients when you added filter order and cutoff frequency.

Type in help butter in the command line and you will find from MATLAB Help why the sample rate was divided by two. “[b,a] = butter(n,Wn) returns the transfer function coefficients of an nth-order lowpass digital Butterworth filter with normalized cutoff frequency Wn,” and “[t]he cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate.”

Filter coefficients were stored in Bd and Ad variables.

Digital Filter Terminology – dspGuru by by Iowegian International

Filter Coefficients – the set of constants used to multiply against delayed signal sample values within a digital filter structure. Digital filter design is an exercise in determining the filter coefficients that will yield the desired filter frequency response. For an FIR filter, the filter coefficients are, by definition, the impulse response of the filter.

Filter Order – every single filter from above is characterized by its order (N); this Nthorder is described by the number of reactive elements that realize a filter. For IIR filters, the filter order is equal to the number of delay elements in the filter structure. Generally, the larger the filter order, the better the frequency magnitude response performance of the filter.

Another important aspect of this filter is the frequency response – how this filter affects the spectral components. The ideal frequency response of a filter should be a perfect rectangle with an amplitude 1 and a cutoff frequency 0.5. In real life, all types of filters can be chosen for your application.

The picture below is shows the frequency response of a digital filter. In our case, the passband has a flat amplitude without ripple. The cutoff frequency is 3400 Hz, which means that our signal was filtered with a low-pass and its frequency shouldn’t be over this limit. As you can see in the picture, the cutoff frequency isn’t followed by the stopband and that’s why it’s possible to have another frequencies above it. This can be improved by increasing the order of the filter.   

As shown in Figure 10, the Elliptic filter appreciates cut-off frequency the most, but the amplitude is not constant. Chebyshev 1 and 2 have amplitude ripple in the passband and stopband. In our application, we used the Butterworth filter due to its lack of amplitude ripple in both bands; the disadvantage of this filter, however, is its larger transition band.

The order of the IIR filter is smaller than that of the FIR filter.

In our application, the order of the filter was set to 40 in order to have a frequency response as close as to the ideal one. As you can see in Figure 11, as the filter order increases, the performance increases in terms of frequency response.

After we filter and decimate the signal, we need to observe the differences between the original signal and the one that was just transformed. We are going to use a plot and also generate an audio signal.

The simplest way to verify if the results were effective is to look at those signals. The comparison is made between the original signal (48kHz sampling rate) and the final one (16kHz sampling rate). There isn’t much noise on the final signal, which is a very good thing.

arduino dsp

Figure 12: Differences between the initial signal vs filtered-decimated signal

In Part 2, our goal was to examine the characteristics for the digital filters and test them using MATLAB. Until now, we’ve managed to learn about sampling rate and how changes affect the audio signal. In the next part, we’ll approach audio signals from a different angle using Fourier Transform (i.e. frequency domain).

Tiberia Todeila
Tiberia Todeila
Tiberia is currently in her final year of electrical engineering at Politehnica University of Bucharest. She is very passionate about designing and developing Smart Home devices that make our everyday lives easier.

Check us out on Social Media