Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All...

5
1 Signals Outline • Announcements: – Homework III: due Today by 5, by e-mail • for P.4: n can be anything you want – HW IV available soon. • Binary Files • Signals, signals, signals Binary Basics All computer files are “binary”, that is composed of 0’s and1’s When the computer reads ASCII files, it takes chunks of 8 bits (1 byte) and looks up the character To save pi to 16 digits takes 18 bytes in ASCII If you save the 1’s and 0’s that correspond to the double precision value of pi, that takes only 8 bytes

Transcript of Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All...

Page 1: Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All computer files are “binary”, that is composed of 0’s and1’s •When the computer

1

Signals

Outline

• Announcements:– Homework III: due Today by 5, by e-mail

• for P.4: n can be anything you want

– HW IV available soon.

• Binary Files• Signals, signals, signals

Binary Basics

• All computer files are “binary”, that is composed of 0’sand1’s

• When the computer reads ASCII files, it takes chunks of8 bits (1 byte) and looks up the character

• To save pi to 16 digits takes 18 bytes in ASCII• If you save the 1’s and 0’s that correspond to the

double precision value of pi, that takes only 8 bytes

Page 2: Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All computer files are “binary”, that is composed of 0’s and1’s •When the computer

2

• You can’t just look at them• You must know exactly how they were

created– integers vs. floating point– single precision vs. double precision– signed vs. unsigned

Problem with Binary Files

• fid=fopen(fname,’r’);%’r’ = read binary• A=fread(fid,N,precision)

– N=number of data points, use Inf to readeverything

– precision is how the file was created• “uint64” is an unsiqned integer saved in 64 bits• “double” is a double

Reading Binary files

Free advice (you get whatyou pay for)

• The only reasons to use binary files are– someone gives you one– you enjoy frustration and pain– you’re too poor (or cheap) to buy a new

hard drive

Page 3: Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All computer files are “binary”, that is composed of 0’s and1’s •When the computer

3

Writing .mat files outsideMatlab

• .mat files are a great format for storing data– easy to use with Matlab– multiple variables / file– compact

• It is possible to save data to .mat files fromC/C++ programs using Matlab C/C++ library

• For more info:– See Lecture 09 notes– Take CIS404!

Signals

• Signals are time series– Examples:

• Sound (pressure vs.time)

• Earthquake(displacement vs. time)

• S&P 500 ($/share vs.time)

• Signals are usuallycontinuous, but wesample them at discretetimes– regular vs. irregular

sampling– Sampling frequency

Signal Basics

• Simplest signal: s(t)=A*sin(2*pi/f*(t-phi))– A=amplitude– f=frequency– phi=phase

• A,f,phi summarize signal

1/f

A

phi

Page 4: Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All computer files are “binary”, that is composed of 0’s and1’s •When the computer

4

Fourier Analysis

• Real signals are more complicated• Fourier proved that any function can be

represented as sum of sines & cosines ofvarious frequencies:

f=(k-1)/N

Fourier Analysis

s1(t)

s2(t)

0.5*s1(t)+s2(t)

f=8

f=1/2

Signals in MATLAB

• In MATLAB, a signal is a vector of numbers s• Matlab’s signal processing functions assume

– s was sampled regularly– s is complete (no missing data, nans, -999’s etc.)

• You must know sampling frequency f

Page 5: Signals - Cornell University•Binary Files •Signals, signals, signals Binary Basics •All computer files are “binary”, that is composed of 0’s and1’s •When the computer

5

Fourier Analysis

• Fourier transform (fft)– Finds amplitudes over a range of frequencies

• amp=fft(s);

– If s is n-by-1, amp will be n-by-1 and complex•

– First half of amp contains info:• a=real([amp(1),2*amp(2:n/2)])/n; %cos coefs.• b=imag([0, -2*amp(2:n/2)])/n; %sin coefs.• f= (0:(n/2-1))/n/(t(2)-t(1)); %frequencies• F=2*pi*t(:)*f;• s2=cos(F)*a(:)+sin(F)*b(:); %original signal

Fourier Analysis

• What’s the point?– fft transforms from time-domain to

frequency domain• Energy at frequency j = sqrt(a(j).^2+b(j).^2)• Plot energy vs. f• Peaks are important f’s• Could remove energy at some frequencies

Signal Processing Toolbox

• Matlab’s Signal Processing Toolboxcontains lots of functions for workingwith digital signals– transforms beyond fft– filter design, implementation– spectral analysis– Check on-line help for more info– Need to understand theory better than I

do!