30
Computational Methods (PHYS 2030) York University Winter 2018 Lecture 25 Instructors: Prof. Christopher Bergevin ([email protected]) Schedule: Lecture: MWF 11:30-12:30 (CLH M) Website: http://www.yorku.ca/cberge/2030W2018.html

Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Computational Methods (PHYS 2030)

York UniversityWinter 2018Lecture 25

Instructors: Prof. Christopher Bergevin ([email protected])

Schedule: Lecture: MWF 11:30-12:30 (CLH M)

Website: http://www.yorku.ca/cberge/2030W2018.html

Page 2: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

à Consideranemptybulletinboard….

A ”computer vision” problem…

“thestapleproblem”

Page 3: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Ø Howmanystaplearethere?

Ø Distributionofangles?

Ø Howtowriteacodetoobjectivelymeasuresuch?

A ”computer vision” problem…

Page 4: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXquantizeF.m

0 100 200 300 400 500 600 700−350

−300

−250

−200

−150

−100

−50

0

freq. [Hz]

mag

nitu

de [d

B]

non−quantized versionquantized versionWindowed non−quantized

Ø Asurprisinglylargeeffectisapparenthere![NotethattheverticalaxisisindB,whichislogarithmic]

Page 5: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

DC vs AC

Ø DC (or‘directcurrent’)typicallyreferstostaticconditions(i.e.,|w|=0)

Ø AC (or‘alternatingcurrent’)typicallyreferstosinusoidalconditions(i.e.,|w|>0)

Ø Thissimpledistinctionhasawidevarietyifimplicationthroughoutphysicsandengineering

Ex.Ohm’slaw

V = IR DCversion

V = IZ ACversion(allvariablesarecomplexFouriercoefficients)

à Here,Z isknownastheimpedance (we’llcomebacktothis)

Page 6: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXspectrogram.mSpectrograms

Stevens(2000)0 1 2 3 4 5 6 7 8

−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Time [s]

Pres

sure

[arb

]

time waveform

‘time waveform’ recorded from mic

Spectrogram=ShortTimeFourierTransform(STFT)

Page 7: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

STFT

Kutz (2012)

Ø BasicideaistocomputeDFTovershortintervals(i.e.,shortsegmentsfromalongerwaveform)andtherebyseehowfrequencycontentischangingwithtime

Ø Clearlythereisatradeoffbetweentimeandfrequencyresolution

P.windowL= 2048; % length of window segment for FFT {2048}P.overlap= 0.8; % fractional overlap between window, from 0 to 1 {0.8}pts= round(P.windowL*P.overlap); % convert fractional overlap to # of pointsspectrogram(A,blackman(P.windowL),pts,P.windowL,P.SR,'yaxis');

‘overlapping’andwindowingcanhelp

EXspectrogram.m

Page 8: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Summary

Uncertainty– Localizationinonedomainmeansbroadeningintheother

Informationcanrepresentedindifferentwaysbydifferent‘basisfunctions’

Practically,therewillbevarioustrade-offsThreesidesofthesamecoin

Fourierseries

Fouriertransform

DiscreteFouriertransform

Page 9: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

à Consideranemptybulletinboard….

A ”computer vision” problem…

“thestapleproblem”

Page 10: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Ø Howmanystaplearethere?

Ø Distributionofangles?

Ø Howtowriteacodetoobjectivelymeasuresuch?

A ”computer vision” problem…

Page 11: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system
Page 12: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Ex. Photoshop & ‘sharpening’

Page 13: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

à Howdoesthe‘sharpen’tool(orSharpen‘filter’)workinPhotoshop?

Ex. Photoshop & ‘sharpening’

Page 14: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Devries(1994)

Ex. Electric circuit’s ‘impulse response’

Ø Considerarelativelysimpleelectriccircuit:

Vin(t) Vout(t)Ø Wewillattempttocharacterizethis

(linear)‘system’intermsofthe‘signals’Vin(t) andVout(t)

Ø Consideriftheinputsignal[Vin(t)]isanimpulse.....

Ø Thecircuithaselementsthatwilldynamicallyshapetheinput(e.g.,energywillchargeupthecapacitor,theresistorwilllaterdissipatestoredenergysuchthatavoltagepersistsaftertheinputsignalpasses,....)

Ø ...Sotheoutput[Vout(t)]ofthesystemdiffersretheinput

Page 15: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Devries(1994)

Ex. Electric circuit’s ‘impulse response’

Ø Itappearsthattheproperties/characteristicsofthesystemarethusmanifestsomehowintherelationshipbetweentheinputandtheoutput

Ø WecanuseKirchoff’s lawtoexplicitlydeterminesucharelationship

§ Assumecurrentflowsinaclock-wise(CW)direction.Thenthecurrentflowingintopointb is:

§ Thecurrentthroughthecapacitorisproportionaltotherateofchangeofthepotentialdifferenceacrossit:

§ ButKirchoff’s currentlawrequires:

§ Forsimplicity,letVc=0.ThenVa=Vin andVb=Vout .Then:

Page 16: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Devries(1994)

Ex. Electric circuit’s ‘impulse response’

Ø Wethusendupwithasingle,linear,non-autonomousfirst-orderODE.Thegeneralsolutionis:

Specificsolution:

à Keepthisexampleinmind.We’llcomebacktothisshortly....

Ø Nowlet’sconsidertheinputasanimpulse:

Vin(t) Vout(t)

Page 17: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Convolution

Ø Adeceptivelycomplexquantity.Considertwofunctionsp(t)andq(t).Thentheconvolutionisdefinedas:

Notethatthisquantitywillbeafunctionoft (t isthe‘sliding’variablehere)

wikipedia (convolution)Devries(1994)

à Fromacomputationalpointofview,arelativelysimpleoperation

Page 18: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXconvolution1.m

% ### EXconvolution1.m ### 11.07.14% Example code to perform convolution between a two user-defined binary sequencesclear% --------------------------------wf1=[0 0 0 1 1 1 0 0];wf2= [0 0 0 0 0 0 0 0 1 1 1 0 0 0 0];% --------------------------------% Use custom code (convolve1.m) or Matlab's built-in function? % [should return identical answers]if 1==1

C= convolve1(wf1,wf2); % custom codeelse

C= conv(wf1,wf2); % Matlab's built-in functionend% +++figure(1); clf;subplot(211)stem(wf1,'b'); hold on; grid on;stem(wf2,'diamondr','filled'); xlabel('Sample #'); ylabel('Amplitude');subplot(212)stem(C,'k'); hold on;

Page 19: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

convolve1.m

function y= convolve1(wf1,wf2); % convolve two 1-D row vectors (should work similar to Matlab's conv.m) % +++error(nargchk(2, 2, nargin)), error(nargoutchk(0, 1, nargout))if ~isvector(wf1) || ~isvector(wf2)

error('Parameters must be vectors.')end% ensure they are row vectorsif (~isrow(wf1)), wf1= wf1'; endif (~isrow(wf2)), wf2= wf2'; endm = length(wf1); n = length(wf2); % extract relevant dimensions% create new arrays as needed for operationg= fliplr(wf2); % flipped wf2f= [zeros(1,n) wf1 zeros(1,n)];NN= m+n-1;for k=1:NN

% Note: It took me awhile to get this code right!y(k)= sum(f.*[zeros(1,k) g zeros(1,m-k+n)]); % shifted wf2

endreturn

à PresumablyasimplifiedversionofMatlab’s built-inconv.m

Page 20: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXconvolution1.m

0 5 10 150

0.2

0.4

0.6

0.8

1

Sample #

Ampl

itude

0 5 10 15 20 250

0.5

1

1.5

2

2.5

3

wf1=[0 0 0 1 1 1 0 0];wf2= [0 0 0 0 0 0 0 0 1 1 1 0 0 0 0];C= convolve1(wf1,wf2);

Page 21: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXconvolution1.m

0 5 10 150

0.2

0.4

0.6

0.8

1

Sample #

Ampl

itude

0 5 10 15 20 250

0.5

1

1.5

2

2.5

3Ø Differenceinaxes?

Page 22: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXconvolution1.mwf1=[0 0 1 1 1 0 0];% y= exp(-5*x) for x=[0,1]wf2= [0 0 0 0 0 0 1.0000 0.6065 0.3679 0.2231 0.1353 0.0821 0.0498 0.0302 0.0183 0.0111 0.0067];wf2= [0 0 0 0 0 0 0 0 1 1 1 0 0 0 0];C= convolve1(wf1,wf2);

0 2 4 6 8 10 12 14 16 180

0.2

0.4

0.6

0.8

1

Sample #

Ampl

itude

0 5 10 15 20 250

0.5

1

1.5

2

Page 23: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

% ### EXconvolution2.m ###% Example code to perform convolution between a sinusoid and narrow digital pulseclear% --------------------------------SR= 44100; % sample rate [Hz]Npoints= 8192; % length of fft window (# of points)f= 2580.0; % wf1: Frequency (for waveforms w/ tones) [Hz]CLKbnd= [1000 1001]; % wf2: indicies at which pulse turns 'on' and then 'off'% --------------------------------% +++t=[0:1/SR:(Npoints-1)/SR]; % create an array of time points% +++% create two waveforms (same dimensions)wf1= cos(2*pi*f*t);clktemp1= zeros(1,Npoints);clktemp2= ones(1,CLKbnd(2)-CLKbnd(1));wf2= [clktemp1(1:CLKbnd(1)-1) clktemp2 clktemp1(CLKbnd(2):end)];% +++% Use custom code (convolve1.m) or Matlab's built-in function? [should return identicalanswers]if 1==1

C= convolve1(wf1,wf2); % custom codeelse

C= conv(wf1,wf2); % Matlab's built-in functionend% +++figure(1); clf;subplot(211)plot(t,wf1,'b'); hold on; grid on;plot(t,wf2,'r'); xlabel('Time [s]'); ylabel('Amplitude');subplot(212)plot(C,'k'); hold on;

EXconvolution2.m

Page 24: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

EXconvolution2.m

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2−1

−0.5

0

0.5

1

Time [s]

Ampl

itude

0 2000 4000 6000 8000 10000 12000 14000 16000 18000−1

−0.5

0

0.5

1

0 2 4 6 8 10 12 14 16x 10−4

−1

−0.5

0

0.5

1

Time [s]

Ampl

itude

9130 9140 9150 9160 9170 9180 9190 9200 9210 9220−1

−0.5

0

0.5

1

Zoomed-inversion

à Interestingly,youseemtogetbacksomethingthatlookslike(aslightlymodifiedversionof)theoriginal.....[thisrelatesbacktowhywesamplewithpulses!]

Page 25: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Connecting back to the electric circuit

Ø Whatistheconnectionbetweenthecircuitandtheconvolution?

Ø Imaginethattheinputisastringofimpulses.Sincethesystemislinear,theoutputwilljustbeasumofallthe‘individual’responses

Singleimpulse Twosuccessiveimpulses

Ø Wecanimaginealimitwithmoreandmoreimpulsesarrivingatshorterandshorterinterval,therebypassingtoacontinuouscase

Thereisa‘history’!

Herewehavejustwrittena(arbitrary)continuousfunctionasanintegraloverdeltafunctions

Page 26: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Connecting back to the electric circuit

Ø Letr(t)representthe‘impulseresponse’ofthesystem(i.e.,theresponsetoadeltafunctioninput)

Ø Writingourinputvoltageas...

Ø ...leadstoanexpressionfortheoutputas:

à Thatis,theoutputofthesystemistheconvolutionoftheinputsignalandthesystem’simpulseresponse

Putmoregenerally,theimpulseresponsetotallycharacterizesthe(linear)system!

Ø Remarkably,allthistiesverynicely/importantlybacktoFouriertransforms....[we’llcomebacktothatinabit]

Page 27: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Ø Intuitivelydefinedintwodifferent(butequivalent)ways:

1. Timeresponseof‘system’whensubjectedtoanimpulse(e.g.,strikingabellw/ahammer)

2. Fouriertransformofresultingresponse(e.g.,spectrumofbellringing)

ex.Harmonicoscillator Temporalimpulseresponse

Spectralimpulseresponse

Impulse response

(Important)Note:TheFouriertransformoftheimpulseresponseiscalledthetransferfunction

Page 28: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

Devries(1994)

Ex. Electric circuit’s ‘impulse response’

Ø Howdoesthisalltieback(intuitively)toouroriginalcircuitproblem?

Vin(t) Vout(t)Ø Fouriertransformoftheimpulseresponse

‘attenuates’athighfrequencies

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.80

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Time [ms]

Sign

al

Time Waveform

[via EXspecREP3.m]SR= 44100;Npoints= 8192; RC= 0.0001;

10−3 10−2 10−1 100 101 102−80

−75

−70

−65

−60

−55

Mag

nitu

de [d

B]

Spectrum

0 5 10 15 20 25−0.2

−0.15

−0.1

−0.05

0

Frequency [kHz]

Phas

e [c

ycle

s]

à Low-passfilter

à Thisintroducesanimportanttopicknownas‘filtering’

Page 29: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system

à Howdoesthe‘sharpen’tool(orSharpen‘filter’)workinPhotoshop?

Ex. Photoshop & ‘sharpening’

Page 30: Computational Methods (PHYS 2030) · 2018-03-19 · Devries (1994) Ex. Electric circuit’s ‘impulse response’ Ø It appears that the properties/characteristics of the system