engr101: lecture 7 · engr101: lecture 7 march 201915/25. what is the goal of the project? (2)...
TRANSCRIPT
ENGR101: Lecture 7Signals, siganal processing basics
March 2019
ENGR101: Lecture 7 March 2019 1 / 25
What we cover today?
• Signals - analog and digital
• C++: branching and cycles
• Working with files
• Project 1 introduction
ENGR101: Lecture 7 March 2019 2 / 25
Project
• You write a software which produces audio files
• Play the files and visualize air pressure (there is a software for that)
• We made it easier for you - you don’t have to follow all the bells andwhistles of audio format. All you have to do is to generate array ofintegers.
• This software has to generate the signal.
You will need:
• Make new C++ project
• include library functions into your project
• Make some parts of code run repeatedly (cycles)
• Make code perform different actions depending upon some condition
ENGR101: Lecture 7 March 2019 3 / 25
What is the signal?
• Anything that carries information can be called a signal.
• Information in most basic form is a measure of surprise. Thereshould be a change otherwise there is no new information.
• Any kind of changing physical variable can be a signal. Sound is asignal – changing air pressure as sensed by an ear.Image is a signal – light intensity and colours are perceived by the eye.
ENGR101: Lecture 7 March 2019 4 / 25
Signal
Question: Which statement contains more information?
1 It is snowing in Sahara today
2 Today is windy day inWellington
ENGR101: Lecture 7 March 2019 5 / 25
Analog signal
• Analog signal is continuous - nomatter how much you zoom inyou will see smooth curve
• We perceive everything as ananalog signals - if changes arenot too fast
• But computers work withnumbers and they do steps intime.
ENGR101: Lecture 7 March 2019 6 / 25
Sampling
• First step is to measure signal atfixed moments of time.
• A sample is a value at a point intime.
• Samples are taken every Tseconds
• The sampling frequency fs orsampling rate, fs, is the averagenumber of samples obtained inone second (samples persecond), thus fs = 1/T.
• If samples are taken at rate 10per second (T=0.1 sec), forexample, frequency is 10 Hz.
ENGR101: Lecture 7 March 2019 7 / 25
Sampling rate
• How fast samples should be taken?
• Music examples: 1024 samples per second and 44 100 sps (samplesper second)
• Human ear can hear acoustic signals up to 20 000 Hz
• NYQUIST THEOREM:To detect a signal at frequency f, you must digitize at a rate of fasterthan 2f. To detect 1000 Hz, must digitize at > 2000 samples persecond (minimum).
Music: Courtesy http://music.columbia.edu/cmc/musicandcomputers/chapter2/02 03.php
Why we hear low tones?
ENGR101: Lecture 7 March 2019 8 / 25
Nyquist theorem
• Sampling frequencyshould be high enoughso that samples followoriginal signal
• If sampling frequency isnot high enoughdigitized signal isdistorted
• It will lose all higherfrequencies
• Output will have lowfrequency signals whichare not in original signal
ENGR101: Lecture 7 March 2019 9 / 25
Analog to Digital Conversion (ADC)
• Process we went through isAnalog-to-Digital-Conversion
• ADC converts continuous(analog) input signal intostream of binary numbers
• Usually it does sampling andquantization
• Output is distorted signal(quantization noise) becausebinary output do not representinput signal exactly
ENGR101: Lecture 7 March 2019 10 / 25
Another way around - Digital To Analog conversion
Figure:
Simple sequence of numbers (rawdata) is not enough. You have toexplain to DAC what is time intervalbetween samples.
ENGR101: Lecture 7 March 2019 11 / 25
Audio file formats
http://soundfile.sapp.org/
doc/WaveFormat/
To restore waveform from set ofnumber we need to know more:
• Sampling rate
• For music - is it mono or stereo
• How many bytes per sample
• and what not...
All this information is contained inheader (it is not h file as in C++).First lines in the file contain thisinformation.
ENGR101: Lecture 7 March 2019 12 / 25
Open wav file in text editor
Good news is that youdon’t have to writethis part.
ENGR101: Lecture 7 March 2019 13 / 25
Reminder - what you may need to complete the project?
• Working with arrays
• Reserving memory for the arrays (initializing)
• Traversing (cycles)
• Working with files (challenge part)
ENGR101: Lecture 7 March 2019 14 / 25
What is the goal of the project? (1)
• Software should generate arrayof samples (red in picture).Hardware(speaker) will smoothit (can not move very fast) andair pressure will follow blue line(can call it envelope)
• First of all we need to decidewhat is time interval betweensamples (dt). Good qualitysound can be produced if we use44100 samples per second.
ENGR101: Lecture 7 March 2019 15 / 25
What is the goal of the project? (2)
• 44100 samples per secondmeans that samples shouldfollow every dt = 1/44100seconds
• Select duration of the sound T
• If duration is T seconds andsamples should be dt secondsapart then total number ofsamples is:
N = T/dt (1)
ENGR101: Lecture 7 March 2019 16 / 25
What is the goal of the project? (3)
• We know now how manysamples we should have - N
• Now we calculate amplitude(volume) of each sample
• What is time of sample i?OK, it is sample number i,samples are dt apart, so it is
ti = i ∗ dt (2)
• Volume at this moment oftime? Say, we want simpletone:
a(t) = A · sin(ωt) (3)
ENGR101: Lecture 7 March 2019 17 / 25
Array of samples
There is an equation which describes waveform:
a(t) = A · sin(ωt) (4)
, where A is an amplitude (volume) of the sound,ω is the frequency. ω = 2 · π · f and f is frequency [Hz].t is time calculated by:
ti = i ∗ dt (5)
and i is changing from 0 to N Putting it all together:Array of samples is:
a[i ] = A ∗ sin(2 ∗ π ∗ f ∗ i ∗ dt) (6)
To use sin and π you have to include math.h. We have to do calculationsfor i changing from 0 to N. How?
ENGR101: Lecture 7 March 2019 18 / 25
Iterations
Sometimes we need to repeat calculations several times.Say, we want to print numbers from 1 to 6.
Figure:
There is shortcut for it:
for (int i = 1 ; i < 6 ; i= i+1)
{
cout <<i<<endl;
}
// carry on
All shown in green logic is implemented byone line:
for (int i = 1 ; i < 6 ; i= i+1)
ENGR101: Lecture 7 March 2019 19 / 25
Question?
#include <iostream >
using namespace std;
int main (){
for (int i = 0 ; i < 6 ; i=i+2){
cout <<i<<" ";
}
}
1 0 2 4
2 0 1 2 3 4 5
3 0 1 2 3 4 5 6
ENGR101: Lecture 7 March 2019 20 / 25
Question, again?
#include <iostream >
using namespace std;
int main (){
for (int i = 10 ; i < 6 ; i=i+2){
cout <<i<<" ";
}
}
1 0 2 4
2
3 10 8 6 4 2 0
ENGR101: Lecture 7 March 2019 21 / 25
Question, again?
#include <iostream >
using namespace std;
int main (){
for (int i = 0 ; i < 6 ; i=i+1){
if ( i > 2 ){
cout <<i<<" ";
}
}
}
1 0 2 4
2 3 4 5
3 0 1 2 3 4 5
ENGR101: Lecture 7 March 2019 22 / 25
How to to work with an array of ints?
Usually you use for() to work with arrays.
#include <iostream >
using namespace std;
int main (){
int a[5];
for ( int i = 0 ; i < 5;i = i + 1){
a[i] = i*2;
}
for ( int i = 0 ; i < 5;i = i + 1){
cout <<a[i]<<" ";
}
return 0;
}
1 0 2 4 6 8
2 3 4 5
3 0 1 2 3 4 5
ENGR101: Lecture 7 March 2019 23 / 25
Working with files
#include <iostream >
#include <fstream >
using namespace std;
int main (){
string line;
ifstream myfile;
myfile.open("file.txt");
while(getline(myfile , line)) {
cout <<"From file="<<line <<endl;
}
}
• We used iostream -input/output fromkeyboard/display
• fstream class to performinput/output ofcharacters from file
• You open the file first
ENGR101: Lecture 7 March 2019 24 / 25
Questions?
ENGR101: Lecture 7 March 2019 25 / 25