0% found this document useful (0 votes)
34 views12 pages

Real Time Digital Signal Processing Using Matlab: Jesper Nordström

The document describes the development of a real-time digital signal processing program using Matlab. The program samples audio from a computer microphone, allows the user to design and implement digital filters, and plays the filtered audio back through computer speakers in real-time. The program works well and allows users to easily create filters and filter signals, though filtering music requires more precise filters. The program could be improved by enhancing the user interface and filter design tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

Real Time Digital Signal Processing Using Matlab: Jesper Nordström

The document describes the development of a real-time digital signal processing program using Matlab. The program samples audio from a computer microphone, allows the user to design and implement digital filters, and plays the filtered audio back through computer speakers in real-time. The program works well and allows users to easily create filters and filter signals, though filtering music requires more precise filters. The program could be improved by enhancing the user interface and filter design tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Real time digital signal processing using

Matlab

Jesper Nordström
Abstract
Real time digital signal processing using Matlab

Jesper Nordström

Increased usage of electronic devices and the fast development of microprocessors


has increased the usage of digital filters ahead of analog filters. Digital filters offer
great benefits over analog filters in that they are inexpensive, they can be
reprogrammed easily and they open up whole new range of possibilities when it
comes to Internet of things.

This thesis describes development of a program that can sample music from the
computer's microphone input, filter it inside the program with user built filters and
reconstruct the music to the computer's headphone output meaning that the music
can be played from the speakers. All of this is to happen in real time. The program
is developed for students studying at the department of ``Signals and Systems"
and the program is supposed the be one of the educational tools to make sense of
signals and filtering.

The program works well and filters the sound with satisfying results. It is easy to
create filters and filter the signal. Since it is music that is filtered constructing
perfect filters with minimum ripple, minimum or linear phase is quite difficult to
achieve. The program could be improved by improving the user interface, making
the environment more interactive and less difficult to construct good filters. Some
improvements could also be made to the implementation; as of now the program
might run a bit slow on startup on slower computers.
Acknowledgements

Jag har engagerat m˚anga v¨anner, klasskompisar och familj f¨or st¨od och input till
det h¨ar projektet. Ett stort tack till mina v¨anner i klassen som alltid finns d¨ar f¨or de
sm˚a fr˚agorna som hj¨alper en att friska upp minnet. Ett speciellt tack till Mattias
Dahlstedt som helt obekymrat bara alltid finns d¨ar och som alltid vet. Tack Filip O¨
stermark som st¨ottade och hj¨alpte med sin stora kunskap i ett omr˚ade d¨ar jag var
ny och obekv¨am. Tack till Rachel Northedge som tog sig tid som hon egentligen saknade
f¨or att ge mig v¨ardefull input. Stort tack till Ping Wu, min handledare som alltid ¨ar
st¨ottande, positiv och framf¨or allt alltid g¨or sitt yttersta f¨or att hj¨alpa en och
ger en k¨ansla av att han vill se en lyckas. Tack till Steffi Knorn min
¨amnesgranskare som gav mycket input i slutet av projektet. En person som inte
g˚ar att tacka nog ¨ar min fantastiska flickv¨an Elvira Bergstr¨om. Utan henne hade jag
inte orkat komma tillbaka fr˚an den tuffa tid som f¨oljde olyckan och som st¨ottar
en med det som beh¨ovs, oavsett om det ¨ar v¨armande ord eller att g¨ora snygga
bilder till rapporten. Ord kan inte beskriva hur tacksam jag ¨ar.

Sammanfattning

V¨arlden vi lever i blir allt mer digital. Snart kommer det att finnas en
microprocessor i varje elektronisk pryl som finns i v˚ara hem, inklusive all
k¨oksapparatur och andra prylar i v˚ara hem som kan drivas med el. I framtiden
kommer m˚anga av dessa prylar ocks˚a vara uppkopplade p˚a n¨atet,
sammanl¨ankat i ett Internet of things. I all elektronisk appa- ratur finns det filter av
olika slag, filter som filtrerar brus, filter som skyddar mer k¨anslig elektronik och filter
som ¨andrar signaler. D¨ar det finns en mikroprocessor s˚a finns ocks˚a m¨ojligheter
att konstruera och anv¨anda digitala filter. Digitala filter har m˚anga f¨ordelar
gentemot analoga filter i det att de ¨ar v¨aldigt billiga att tillverka och implementera, de
¨ar l¨atta att ¨andra egenskaper p˚a bara genom att programmera om mjukvaran och att
de inte
¨andrar egenskaper i takt med att elektroniken blir gammal och sliten. Nackdelar
¨ar att digitala filter inte f˚ar ett lika platt impulssvar och inte heller ¨ar lika
precisa eller billiga som analoga filter i enkla fall. Digitala filter introducerar ocks˚a en
f¨ordr¨ojning i systemet, n˚agot som ¨ar marginellt och f¨orsumbart vad g¨aller analoga
filter.
I detta examensarbete utvecklas ett program som k¨ors i Matlab genom att man
kopplar in en ljudspelande dator eller mobil i mic-uttaget p˚a en dator, samplar det
inkommande ljudet och filtrerar ljudet i programmet. Efter att ljudet blivit filtrerat s˚a
kan ljudet spelas upp genom h¨orlursuttaget p˚a dator, allt i realtid. Programmet ska
allts˚a fungera som en lite mer avancerad equalizer.
Programmet ¨ar utvecklat i Matlab med hj¨alp av Matlabs DSP toolbox. Syftet ¨ar
att programmet ska kunna anv¨andas i utbildningen p˚a avdelningen f¨or ”Signaler och
II
system”. D¨arav har mycket tanke vigts ˚at att programmet ska vara s˚a pedagogiskt
som m¨ojligt med en s˚a intuitiv GUI som m¨ojligt. S˚a mycket som m¨ojligt av
funktionerna har f¨ors¨okt att vara sj¨alvf¨orklarande s˚a l˚angt det g˚ar och d¨ar
f¨orklaring beh¨ovs finns det info-knappar med enklare f¨orklaringar. Studenter ska
allts˚a kunna starta programmet, tillverka och implementera filter i programmets
milj¨o samt kunna testa dessa filter p˚a riktigt ljud i realtid.
Programmet fungerar v¨al och l˚ater anv¨andaren p˚a ett enkelt s¨att implementera och
tillverka filter. Filtrering av musik ¨ar sv˚art, perfekta filter kr¨avs och det ¨ar inte
enkelt att tillverka filter som inte ger n˚agon som helst distortion. Att vidareutveckla
och g¨ora programmet enklare att implementera p˚a musik genom att ha en kreativ men
¨and˚a vetenskaplig guide till att f˚a filtret s˚a precist som kr¨avs f¨or musik hade
varit bra. Det hade kunnat ¨oka kreativiteten och men samtidigt ocks˚a l¨arandet.

Contents
1 Introduction 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Aim of the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Project description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Project specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Theory 5
2.1 Analog to digital conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Sampling and frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1 Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.2 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Digital filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.1 Finite impulse response filters . . . . . . . . . . . . . . . . . . . . . . 9
2.3.2 Infinite impulse response filters . . . . . . . . . . . . . . . . . . . . . 11
2.4 Discrete Fourier transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.5 Digital to analog conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3 Implementation 17
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2 Hardware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.1 Sound card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.2 Adaption to multiple types of computers . . . . . . . . . . . . . . . . 18
3.3 Software implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3.1 Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3.2 Generation of code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3.3 Filter design algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.4 Graphical user interface . . . . . . . . . . . . . . . . . . . . . . . . . 21

4 Results 27
4.1 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2 Test runs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.1 Two sinuses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.2 Filtering Bach - Orchestral Suite No. 3 (Air) . . . . . . . . . . . . . 30
4.3 Realtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.4 Adaption to multiple computer systems . . . . . . . . . . . . . . . . . . . . 32

III
5 Discussion 36
6 Further development 37

A Syllabus for Signals and Systems course 39

IV
B Main program Matlab code 41
B.1 Main GUI..........................................................................................................41
B.2 FFT plot GUI................................................................................................68
B.3 Time plot GUI.................................................................................................. 75
B.4 Main loop......................................................................................................................82
B.5 Convert string to vector........................................................................................87
B.6 Create y for timeplot............................................................................................. 88
B.7 Create y for timeplot............................................................................................. 89
B.8 Find limits to x-axis in spectrum plot...................................................................89
B.9 Print Matlab filter information..............................................................................90

C Matlab code used in results 91


C.1 Filter function for filtering sinuses in section 4.2.1..............................................91
C.2 Filter function for filtering Bach Air Suite No.3 in section 4.2.2.....................91

V
Chapter 1
Introduction
1.1 Background

In this day and age, as every side of our daily lives shifts more and more into the digital
world, the technology we use is developing fast. According to statistics conducted by the
authority of Swedish statistics (SCB), 93 percent of the Swedish population had access to
the internet in the first quarter of 2016 [page 4 15]. Today 50 percent of the population
listens to music via the internet[page 6 15]. It is not only mobile phones, computers and
tablets that use digital technology anymore. According to Intel’s website more than 200
billion devices will have “smart”-technology with at least a microprocessor in 2020[2].
In almost every kitchen appliance, camera, mobile phone or music playing device there
are filters. Some are simple analog ones that only work as protection for the more delicate
electronics, while others include complicated dj music mixing board filters where one
undesired frequency can destroy an entire song. In mp3-players and various other types of
music-playback devices it is important that the listener can listen to a song without noise,
also different genres of music require different sorts of filtering.
Digital filters will have a large role to play as electronic devices become increasingly
“smart”. Microprocessors open up a whole new range of possibilities when it comes to
filtering. Digital filters are not as precise as analog filters, nor do they have equally as flat
impulse responses. However they are very cost effective, especially if the device already has
a microprocessor. They can also be reprogrammed easily and if the device has an internet
connection, the impulse response can easily be changed with just a software update.
It is often not required for the filter to run in real-time, if the input exists in data files
and can be computed beforehand. However often the input signal is not known, the signal
has perhaps to be sampled, patterns must be recognized and signal operations performed
while the application is running. One example of this is a telephone call with a noisy
background. It will be sampled into the sender’s telephone, filtered and then played in the
ear of the receiver, with the noise filtered to the extent that it is easy to interpret what
the sender is saying. There are other more advanced examples of this, like machine vision
and smart cameras, Biometrics and Grid automation[3].

1.2 Aim of the project


The aim is to create a program that can sample sound from a mic input, perform Digital
Signal Processing (DSP) on the signal and then play it using the computer’s headphone
output. The program is to be used as a supplement to laboratory exercises and lectures
and thus be an educational tool. The program should not be too heavy on the computers
and pedagogic to some extent to make it accessible.

1.3 Project description


This program should be created so that it can sample from the mic input of an ordinary
personal computer, filter it through some variety of filters, either some of the built in
filters that already exists in the program or by creating a filter with coefficients of the
users preferences. The program is thought of as an educational tool to complete the
already existing laboratations and homework studies in the course “Signals and Systems”
at the department of Signals and Systems at Uppsala University, a syllabus can be found
6
in Appendix A.
There are a variety of functions that this program should offer, among which the most
important ones are listed below.
• Sample and perform operations on sound either from mic input or file input.
• Play audio back to the user from the computer’s headphone output.
• Change the sampling rate of the sound that is sampled from mic.
• Read out the time plot of the sampled signal.
• Read out the frequency spectrum of the signal.
• Create a filter from a custom filter design by typing in the coefficients of the filter.
This works for both IIR and FIR filters as will be seen in Section 2.3.
• Create a filter by specifying the type of filter, sampling frequency, cut off frequency
etcetera and further on apply the filter to the input signal.
• Apply a function to the filter. This opens up the opportunity to create more ad-
vanced filters, cascaded filters or filters that invite to play with the sound signal,
thereby encouraging interest in the subject from students.
The usage of the program is supposed to be straightforward and a block diagram
describing the basics of the program is shown below in Figure 1.1.
1. Choose whether the source should be from a file or from mic input.
(a) If file: type in the file name in the edit file name text box.
(b) If mic: set the desired sample rate in the sample rate text box.
2. Construct your own filter by typing in the coefficients of the numerator and the
denominator in their respective text box. The default transfer function should be
(1/1), in other words no filtration at all.
3. Optional: construct a Matlab filter using the built in filter construction algorithm
“designfilt” from Matlab.
4. Optional: choose one of the prefabricated filters from the pop-up menu.

7
Figure 1.1: A flowchart of how the main GUI should be used.

5. Specify the gain to the output. The default value is 1 (no gain).
6. Press the on-button and play the music. Filter by switching between the different
types of filtration and activate the filtration by pressing the apply filtration tick box.
It should also be possible to pause while the program is running. Either by pausing the
entire program or by pausing the FFT plot or time plot alone. Pausing the FFT plot
or time plot is merely to give the program some increased performance. Pausing the
program should allow the user to freeze the song at a particular moment and acquire
some information about the signal.

1.4 Project specifications


There are no precise technical numbers specifications for the project. However as the
program is to be used for educational purposes, and with students that have different
types of computers, it would be favourable if the program was not too computationally
demanding.
The program should also include some prefabricated filters, at least one IIR-filter per type
(bandpass, lowpass, highpass) as well as at least one FIR-filter per type.

Chapter 2
Theory
In this chapter the essential underlying theory of which the program is based on is dis-
cussed. The outline of the chapter is written in the same order as the theory is used in the
program. Starting with some theory about analog signals, continuing on how the signal is
sampled and quantized into a digital signal inside the computer’s sound card and further
on how the signal is processed inside the computer and played back to the user. A block
diagram describing the system is shown below in Figure 2.1.

8
There are three types of signals; continuous-time analog signals, discrete-time signals, and
digital signals[6]. The world we live in is analog, the physical signals such as speech and
sound are continuous-time analog signals that propagate through some medium, typically
air, and then affect our hearing sense through vibrations. The ear has a complicated
system of ear canals and fluids which are manipulated by hairs that vibrate in the same
frequency as the tone. There are nerves that send signals to the brain depending on the
vibrations and the pressure of the fluids[10].

2.1 Analog to digital conversion


A computer can only store or handle binary data. The analog signal has to be sampled
and be converted to digital data which is an operation by the Analog to Digital Converter
(ADC) in the computer’s sound card. This is done by taking samples of a continuous-time
signal, see Figure 2.2a. This will be a discrete-time signal and could be, not always, a

Sound signal Mic input Sampling

FFT DSP Divide samples into frames

Reconstruction Headphone output Filtered sound signal

Figure 2.1: A block diagram describing the entire system including the Digital Signal
Processing (DSP).

digital signal[6]. After the sampling process the signal has to be quantized, meaning that
the signal at time xd(t0) has to be converted into its closest digital representation. The
sampling process is described by Equation 2.1[4].

Σ
xd(t) = xc(n∆t) δ(t − n∆t) (2.1)
n

The ∆t in Equation 2.1 is the time in between the impulses. xd and xc is the discrete-time
and continuous-time signal respectively. From the convolution theorem a multiplication
in the time domain as in Equation 2.1 corresponds to a convolution in the frequency
domain[4].

Σ
Xd (ω) = Xc (ω)∗F{ δ(t − n∆t)} (2.2)

Sampling a signal using Equation 2.1 is the same as convolving the signal Xc(w) (illustrated
in Figure 2.3a) with the spectrum of the impulse train F{Σ δ(t − n∆t)} (illustrated in
Figure 2.3b). The result is illustrated i Figure 2.4.

9
(a) Sampling process. (b) Quantization process.

Figure 2.2: The figures illustrate the sampling process and the quantization process.

(a) Signal spectrum.


(b) Impulse trai

10
Figure 2.3: The figures illustrate the two signals that are being convolved.

Figure 2.4: Fourier transform of the sampled signal.

2.2 Sampling and frames


For the Digital Signal Processing to be executed in real time there are some important
criteria to be met, considering the rate at which samples are taken and the size of the
frames that store these samples.

2.2.1 Sampling
The ∆t constant in Equations 2.1 and 2.2 specifies at which rate the signal is sampled
at. According to the Nyquist theorem, the sampling frequency fs must be choosen at
least twice as large as the bandwidth of the signal (fM ) to allow the original signal to be
constructed perfectly from the samples. The Nyquist theorem is stated below in Equation
2.3[16].
fs≥2fM (2.3)

If the sampled signal would have contained higher frequencies than fs/2 then the signal
would not be able to be constructed perfectly anymore. The signal would suffer from
aliasing distortion meaning that the frequencies higher than fs/2 would be interpreted as
lower frequencies[13]. Equation 2.4 shows how this periodical property of a sampled
signal can interpret a higher frequency fs/2 +a as a low frequency[16]. It folds higher
frequencies back into the range [−fs/2, fs/2] [11].

fs/2 + a → fs/2 − a (2.4)

This is called aliasing distortion and as Figure 2.5 shows, makes the convolution between
the signal and impulse train overlap itself thereby adding the overlapping parts. Taking
samples more often compresses the impulse train in time domain, but in frequency
domain the impulse train is expanded. Figure 2.5 shows the convolution between the
Σ
signal Xc(ω) and the impulse train F{ δ(t − n∆t)} when there is aliasing distortion[4].

2.2.2 Frames
After the ADC has taken the samples and converted them to be represented with a binary
representation the samples have to be stored. This operation is not performed by the
computer’s sound card, it is performed in Matlab. A common method is to use frames

11
(a) (b)

Figure 2.5: Illustration of overlapping and aliasing.

and let one frame be a finite number of samples. The frames are
then processed one at a time[14] as illustrated in Figure 2.6.

Figure 2.6: Illustration of frame processing.

Frame processing does three things that take time. They are
illustrated in Figure 2.7. There is some time necessary to acquire the
frame which is called overhead, some time necessary to process the
frame and finally a buffer. As long as the overhead and the processing
time is less than the frame time, meaning that there still is some
buffer, the frame is processed in real time.

Figure 2.7: Illustration of frame processing in real time.

12

You might also like