Upload
aldous-mitchell
View
215
Download
0
Embed Size (px)
Citation preview
1
Announcements Take textbook to lab.
» Be sure to read Chapter 13 before lab– MATLAB tutorial
– Can skip Section 13.4
Bring your transmitter and receiver to lab.» 8 AA batteries» 2 9V batteries» Earphones
First Professional Memo Due TODAY!
2
Today’s Topics
Digital signals
Introduction to MATLAB
Generating audio signals with MATLAB
3
Digital Music - Recording
Sampler
-0.0364-0.1884-0.6928-1.7398-2.7642-2.2378 0.1711 1.8333 …
10110101010
byteCD
Analog signal Digital signal
4
Digital Music - Playback
D/AConverter
-0.0364-0.1884-0.6928-1.7398-2.7642-2.2378 0.1711 1.8333 …
CD
Analog signalDigital signal
AudioAmplifier
5
Computer-Generated Signals
MATLAB Sound Card
D/AConverter
AudioAmplifier
0.0000 0.5878 0.9511 0.9511 0.5878 0.0000-0.5878 …
6
Audio Signal
Sound generated by a PC originates as a sequence of numbers (a discrete signal).
0 10 20 30 40 50 60-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 10 20 30 40 50 60-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
What we want What we can generate
7
MATLAB
MATLAB® allows us to generate sequences easily.» The basic data structure in MATLAB is a
vector.» A vector is a convenient way to store a
sequence. We can also play sounds from within
MATLAB.
8
MATLAB Basics
Assign a value to a variable>> a=3
a =
3
>> a=3;>> a
a =
3>>
Semicolon suppresses echo
Typing a variable name displays the value
Be careful!!
9
MATLAB Basics Vectors
>> v=[1,3]v = 1 3
>> v=[1;3]v = 1 3
>> v'ans = 1 3
>> v=1:0.5:3v = 1.0000 1.5000 2.0000 2.5000 3.0000
Comma delimits columns
Semicolon delimits rows
Apostrophe means transpose(turn rows into columns andvice versa)
start value : increment : end value
10
We have been considering sine waves:
is the frequency of the sine wave. Sample the waveform every T seconds.
» Let » We get a sequence » Let n=0,…,N to get sequence corresponding to a
duration of NT seconds.
Generating Sine Waves
tftx 02sin)( 0f
nTt Tnfnx 02sin)(
11
Continuous Sine Wave
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Time in seconds
12
Sampling Operation
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Time in seconds
13
Sampled Signal
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Time in seconds
14
Discrete Array
10 20 30 40 50 60-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Index (n)
15
MATLAB
>> f0=100;>> T=.0008;>> n=0:62;>> x=sin(2*pi*f0*T*n);>> stem(n,x)
Tnfnx 02sin)(
start value : end value (assumes increment of 1)
10 20 30 40 50 60-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
stem(n,x) plot(n,x)
16
Good Programming Habits
>> f0=100;>> T=.0008;>> n=0:62;>> x=sin(2*pi*f0*T*n);>> stem(n,x)
>> x=sin(2*pi*100*.0008*(0:62));>> stem(n,x)
GOOD
BAD!
Do not Repeat Yourself!!
17
MATLAB vs. C C code to create a sine wave:#include <stdio.h>
#include <math.h>
main(argc,argv)
int argc;
char *argv[];
{
int i,n;
double *sv, f0;
n=5000;
f0=100;
sv = (char *) calloc(n, double);
for (i = 0; i < 50000; i++) {
sv(i) = sin(2*3.1415927*f0*I/44100);
}
}
18
Other MATLAB Abilities
Many built-in functions Can easily add your own functions Immediate results without compiling Can solve systems of equations easily All kinds of plotting methods Simulink Maple Symbolic Math Toolbox
19
Sampling Period
If we sample too slowly (T too large), we can get a misleading sequence.
20
Sampled Signal
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
21
Is this the true signal?
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
22
Or, is this the true signal?
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
23
Aliasing
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
24
Sampling Rate Too Low
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
25
“Wagon Wheel Effect”
Correct Sampling
Aliasing
26
Avoiding Aliasing
To avoid aliasing, sample at least twice as fast as the highest frequency in the signal.» Minimum of two samples per period» Shannon’s Sampling Theorem
Example:» A 40 Hz sine wave should be sampled at 80 Hz
or more
27
Playing Sounds in MATLAB
Can play sounds directly from MATLAB:» sound(x,44100)
– x is the sequence of values in a vector
– 44100 is the output sampling rate
» soundsc(x,44100)– Same as sound() but auto-levels before playing
– Each sound played at the same level
28
Playing Sounds in MATLAB
Can read or write WAV files:» y = wavread(‘fast.wav’);
– y is the sound sequence read in as a vector
– fast.wav is the name of the file to be read.
» wavwrite(y,44100,’fast.wav’)
29
Stereo in MATLAB
If x is an Nx2 vector, the left column will be played as the left channel, and the right column will be played as the right channel.» fl=200;» fr=300;» t=[0:1/44100:8];» xl = sin(2*pi*fl*t);» xr = sin(2*pi*fr*t);
sound([xl’ xr’],44100)
30
Quiz 8
1. Long calculations or detailed specifications should be included in the main text of the a report. (True or False)
2. Contractions are acceptable in an engineering report. (True or False)