0% found this document useful (0 votes)
35 views

Conv Mat Lab

The document discusses signal convolution in MATLAB. It provides two example signals, a cosine signal and a rectangular signal, and demonstrates how to convolve them in MATLAB using the conv function. The result of the convolution is plotted and shows five distinct regions corresponding to the overlapping of the two signals over time.

Uploaded by

George Kafentzis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Conv Mat Lab

The document discusses signal convolution in MATLAB. It provides two example signals, a cosine signal and a rectangular signal, and demonstrates how to convolve them in MATLAB using the conv function. The result of the convolution is plotted and shows five distinct regions corresponding to the overlapping of the two signals over time.

Uploaded by

George Kafentzis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sunlixh sto MATLAB

Epimleia: Kafentzc Girgoc Upoy. Didktwr Tmhm. H/U Panepistmio Krthc 22-03-2013

Eisagwg

9iqoume suzhtsei th sunlixh suneqo qrnou sth jewrD kj ki se skseiF e dome se ut ti shmeisei to pw sqedizoume ki epieinoume t potelsmt sto MATLABF prqoun merik shme pou qreizonti prosoq son for thn nprstsh shmtwn suneqo qrnou se n perillon dikrito qronouD opw to MATLABD ki ut j xedilnoume se ut ti shmeiseiF penjumzeti ti h sunlixh eni mi prxh pou sqetizei thn esodo en sustmtoD x(t)D me thn kroustik pkrish tou sustmtoD h(t)D ki thn xodo y (t)D ki dneti p th sqsh

y (t) = x(t) h(t) =

x( )h(t )d

@IA

Smata suneqoc qrnou - Sqedash kai Sunlixh


e dome duo qrkthristik prdegmtF IF 9istw loipn ti qoume duo shmt suneqo qrnouD t

x(t) = cos(t/2), 5 t 5
ki

@PA @QA

h(t) = 2rect(t/2)

pou Arect(t/T ) eni n summetrik tetrgwnik prjuro me plto A ki dirkei T D p T /2 w T /2 secF 9er to sm 2rect(t/2) eni en summetrik tetrgwnik prjuro me dirkei 2 sec @p 1 w 1 secA ki plto 2F tumste ti to MATLAB ngnwrzei le ti metlht tou w pnkeF ypte ol t dedomn pou j eisgoume j eni morf pinkwnD ki sugkekrimnD dinusmtwn @pinkwn N x1 1xN AF rktw dneti kdik gi th sqedsh twn shmtwn sto MATLABX

ts = 0.01; tx = -5:ts:5; x = cos(pi * tx/ 2); th = -2:ts:2; h = 2*rectpuls(th/2); plot(tx, x, th, h);

% % % % % %

Sampling step Time vector for x(t) x(t) Time vector for h(t) h(t) Plot the result 1

xlabel(Time (sec)); ylabel(Amplitude); title(Signals of Example 1); legend(Cosine, Rectangular);


o potlesm fineti sto sqm IF

% % % %

Make Make Make Make

plot plot plot plot

pretty pretty pretty pretty

:-) :-) :-) :-)

Signals of Example 1 2 Cosine Rectangular 1.5

1 Amplitude

0.5

0.5

1 5

0 Time (sec)

qm IX mt rdeigmto 1 werik prthrsei ston prpnw kdikFFF @A r prth grmm m kjorzei to pso suqn prnoume degmt p to suneq smD gi n to nprstsoume sto MATLABF 9ypw gnwrizeteD n sm suneqo qrnou orzeti kje qronik stigmD oi opoe eini peire to pljoD km ki se n kleisto disthm pw pFqF to [0, 1]F rofn den mporome n nprsthsoume le ut ti timF repei n dilxoume kpoieF idD sthn prth grmm epilgoume to m me to opoo j prnoume tim p t smt mF o hm ed eini 0.01 secD to opoo sthn perptwsh m eni rket to m ut exrtti p to suqnotik perieqmeno twn shmtwnD pw j dete pro to telo tou mjmtoF @A th deuterh ki ttrth grmmD orzoume t ped orismo twn shmtwnF o th den eini ngkh n eini diforetik p to txD mw to tx de j mporose n eni dio me to thD giti to x(t) orzeti sto [5, 5]F ipshD to h(t) qei dirkei p 1 w 1D opte h epilog tou distmto [2, 2] m dnei merik mhdenik ektrwjen tou plmoF @gA t mporosme n prosjsoume merik epiplon mhdenik ektrwjen ki sto x(t) ste n proseggizei km perissotero to prgmtik smF e loume 10 deuterolept ektrwjenF eut j gintn kpw etsiX

ts = 0.01; % tx = -5:ts:5; % x = cos(pi * tx/ 2); % x_new = [zeros(1,length(tx)-1) x zeros(1,length(tx)-1)]; % tx_new = -15:ts:15; % 2

Sampling step Time vector for x(t) x(t) Enhanced x(t) New time vector for x(t)

plot(tx_new, x_new, th, h); xlabel(Time (sec)); ylabel(Amplitude); title(Signals of Example 1); legend(Cosine, Rectangular);
rxte ton prpnw kdik gi n dete to potlesmF XEA

% % % % %

Plot Make Make Make Make

the result plot pretty plot pretty plot pretty plot pretty

:-) :-) :-) :-)

@dA r sunrthsh rectpuls() ulopoie nn tetrgwnik plm dirkei 1 deuterolptou ki plE tou 1D sto orismExon pou tou dnetiF r klsh pou knoume edD 2*rectpuls(th/2)D ftiqnei n tetrgwnik prjuro dirkei 2 deuterolptwnD ki pltou 2F @eA yi teleutie 4 grmm pl dnoun mi perissoterh sfnei sth grfik m prstshF whn ti prmeleteD den eni mno gi isjhtiko lgouF XEA e pome oti jloume n knoume sunlixh utn twn duo shmtwnF o oloklrwm th sunlixh dineti w

y (t) = x(t) h(t) =

x( )h(t )d

@RA

r sunlixh ulopoieti sto MATLAB me th sunrthsh convF r sunrthsh ut mw ulopoie th sunlixh gi smt dikrito qrnouF r sqsh @RA mw eni sqsh suneqo qrnouF t prpei n th mettreyoume se mi kpw pio olikD dikrit morfD ste n qrhsimopoisoume thn toimh sunrthsh pou m dinei to MATLABF wporei n deiqje oti h sqsh @RA mporei n proseggiste p th sqsh
k

y (kT ) = x(kT ) h(kT ) = T


i=k0

x(iT )h((k i)T )

@SA

pou kT eni krie qronik stigm n T deuterlept @ousistikD T = ts ston kdikAF r sunrthsh conv ulopoie kri to jroism pou fineti prpnw @qwr th stjer T AF 9er h sunlixh twn duo shmtwn j dineti p ton kdik prktwX

ty = -7:ts:7;

% % y = ts*conv(x,h); % plot(tx, x, th, h, ty, y); % xlabel(Time (sec)); % ylabel(Amplitude); % title(Signals of Example 1); % legend(Cosine, Rectangular, Convolution); %

Convolution time vector [tx1+th1, tx2+th2] Convolution approximation Plot the result Make plot pretty :-) Make plot pretty :-) Make plot pretty :-) Make plot pretty :-)

o potlesm fineti sto sqm PF rosxte oti t kr tou tetrgwniko prjrou @met thn nklsh ki mettopish pou qei uposteD opw qoume dei sth jewriA eni t t 1, t + 1D rister ki dexiD ntstoiqF e lsoume jewrhtik th sunlixhD n dome n sumpptoun t sqmtF t qoume pnte periptE seiD oi opoe fnonti sto sqm QF

rth perptwshX t + 1 < 5 t < 6, y (t) = 0

@TA

Signals of Example 1 3 Cosine Rectangular Convolution

1 Amplitude

3 8

0 Time (sec)

qm PX mt ki unlixh rdeigmto 1

heuterh periptwshX 5 t + 1 t 6 ki t 1 5 t 4 6 t < 4, t+1 t+1 2 y (t) = 2 cos( /2)d = 2 sin( /2) 5 5 4 4 (sin( (t + 1)/2) sin(5/2)) = (sin(t/2 + /2) + 1) = 4 (cos(t/2) + 1) = rth perptwshX 5 t 1 t 4 ki t + 1 5 t 4 4 t 4, t+1 t+1 4 y (t) = 2 cos( /2)d = sin( /2) t1 t1 4 4 = (sin( (t + 1)/2) sin( (t 1)/2) = (cos(t/2) + cos(t/2)) 8 = cos(t/2) trth periptwshX t + 1 > 5 t > 4 ki t 1 5 t 6 4 < t 6 5 5 4 y (t) = 2 cos( /2)d = sin( /2) t1 t1 4 4 = (sin(5/2) sin( (t 1)/2)) = (sin(5/2) sin( (t 1)/2)) 4 = (1 + cos(t/2))
R

@UA

@VA

@WA

Case 1 2 0 2 10 2 0 2 10 2 0 2 10 2 0 2 10 2 0 2 10

0 Case 2

10

0 Case 3

10

0 Case 4

10

0 Case 5

10

10

qm QX eriptsei unlixh rdeigmto 1

mpth periptwshX t 1 > 5 t > 6, y (t) = 0


9er sunolikX

@IHA

0, 4 (cos(t/2) + 1), 8 y (t) = cos(t/2), 4 (cos(t/2) + 1), 0,

t < 6, 6 t < 4, 4 t 4, 4<t6 t>6

@IIA

e sqedisoume ut to sm sto MATLABF hete to sqm RF y kdik pou prgei to sqm R dneti prktwX

t2 = -6:ts:-4; x2 = (4/pi)*(cos(pi*t2/2) + 1); t3 = -4:ts:4; x3 = (8/pi)*(cos(pi*t3/2)); t4 = 4:ts:6; x4 = (4/pi)*(cos(pi*t4/2) + 1); t_zero1 = -7:ts:-6;

% % % % % % % 5

Case Case Case Case Case Case Zero

2 time vector 2 signal 3 time vector 3 signal 4 time vector 4 signal time vector: case 1

Convolution by hand 3

1 Amplitude

1 Case2 Case3 Case4 Zero Case1 Zero Case5 6 4 2 0 2 Time (sec) 4 6 8

3 8

qm RX unlixh rdeigmto 1 E tewrhtik potlesm

t_zero2 = 6:ts:7; % Zero time vector: case 5 x_zero1 = zeros(1, length(t_zero1)); % Zero signal for case 1 x_zero2 = zeros(1, length(t_zero2)); % Zero signal for case 5 plot(t2, x2, t3, x3, t4, x4, t_zero1, x_zero1, t_zero2, x_zero2); xlabel(Time (sec)); ylabel(Amplitude); title(Convolution by hand); legend(Case2, Case3, Case4, Zero Case1, Zero Case5);
rthrste oti to jewrhtik potlesm tou sqmto R ki to potlesm th sunrthsh conv tou sqmto P eni diF XEA PF 9istw ti qoume duo shmt suneqo qrnouD t

x(t) = u(t)
ki

@IPA @IQA

h(t) = 2u(t 2)

pou u(t) eni h gnwsth s hmtik sunrthshF rofnD t smt ut eni peir sto qrnoD opte de gneti n t nprstsoume me krei sto MATLABF t nprstjon meqri n sugkekrimno qronik shmeo th epilog mF e sqedisoume ut t shmt sto MATLAB w eqounX

ts = 0.01; tx = -5:ts:10; x = heaviside(tx); th = -5:ts:10;

% Up to 10 seconds % x(t) = u(t) % Up to 10 seconds again 6

h = 2*heaviside(th - 2); plot(tx, x, th, h); xlabel(Time (sec)); ylabel(Amplitude); title(Signals of Example 2); axis([-5 10 0 3]);

% h(t) = 2u(t-2) % Plot

% Rescale the axis of the figure (prettier graph :-P)

r sunrthsh heaviside() ulopoie mi hmtik sunrthshD nlog me to orism pou th dnetiF r sunrthsh axis prosrmzei t ori twn xnwn sto sqmF to sqm S fneti to potlesm tou prpnw kdikF rD h sunlixh twn duo utn shmtwn j dineti w
Signals of Example 2 3

2.5

2 Amplitude

1.5

0.5

0 5

0 Time (sec)

10

qm SX mt rdeigmto 2

ty = -10:ts:10; % Time vector for y(t) y = ts*conv(x,h); % Convolution (size [-10,20]) y = y(1:length(ty)); % Keep only meaningful result ([-10,10]) plot(tx, x, th, h, ty, y); % Plot xlabel(Time (sec)); ylabel(Amplitude); title(Signals of Example 2); legend(u(t), 2u(t-2), Convolution);
o potlesm fneti sto sqm TF rosxte ston prpnw kdik to exX to dinusm pou ntiproswpeei ton xon tou qrnouD ty D gi th sunlixhD y (t)D den upkoei ston knn pou gnwrizete ki pou edme ki sto prohgomeno prdeigmF oin knnc eutn pou lei oti n pepersmnh dirkei shm x(t)D pou eini mh mhdenik sto [a, b]D otn sunelsseti @koultor XEA A me n llo pepersmnh dirkei sm h(t)D pou eini mh mhdenik sto [c, d]D tte to potlesm tou @h sunlixh dhldhA eini mh mhdenik sto [a + c, b + d]F idD orsme ki t duo smt m sto [5, 10]D pr9 olo pou den eni pepersmn jewrhtikD opw epmeF ent mw n proume w xon qronou ty gi th sunlixh to [10, 20]D prme to [10, 10]3 qitc r pnthsh eni ti se ttoie periptseiD pou npristome peir E jewrhtik E smt sto MATLABD prepei n U

Signals of Example 2 16 14 12 10 Amplitude 8 6 4 2 0 10 u(t) 2u(t2) Convolution

0 Time (sec)

10

qm TX mt ki unlixh rdeigmto 2 elgqoumeGgnwrzoume mqri poi qronik shmeo t potelsmt m qoun nhm @sumpptoun dhld me ut th jewrAF in prokeimnwD xroume ti met th qronik stigm 10 secD to shm m jewreti @p to MATLABA ti mhdenzetiD prgm pou de sumnei sth jewrF ypte Dti potlesm prnoume met p to 10o deuterleptoD den qei nhm3 tewrome ti h grfik prstsh th sunlixh to 10o deuterlepto suneqzeti ep9 peiron sto moto pou eqe w ekenh th qronik stigm @sto prdeigm mD h euje suneqzei n nenei w to peiroAF rosxte oti h grmm

y = y(1:length(ty));

% Keep only meaningful result ([-10,10])

pojhkeei sto dinusm y t stoiqei tou dinsmto y p to 1o w ton rijm pou isoti me to mko tou xon ty pou qoume epilxeiF r sunrthsh length() m epistrfei to mko en dinsmto @dhlF ps stoiqe periqeiAF ipeid h sunrthsh conv() m epistrefei thn plrh sunlixhD jewrnt ti t shmt m mhdenzonti met to 10o deuterleptoD prpei eme n krtsoume t stoiqe tou y pou qoun nhmF eut knoume sthn prpnw grmmF e lsoume th sunlixh nlutikD n dome n sumpptoun t potelsmt mF t eini

y (t) = x(t) h(t) =

x( )h(t )d =

2u( )u(t 2)d

@IRA

9ymw prthrome oti

u(t 2) =
ki

1, t 2 0 t 2, 0, llo 1, 0, 0, llo 2, 0 t 2, 0, llo

@ISA

u( ) =
ypteD p koinoD

@ITA

2u( )u(t 2) =
V

@IUA

9er to oloklrwm m gneti


t2

y (t) = = 2

2u( )u(t 2)d =


t2 0 0

2d
@IVA

= 2(t 2) = 2t 4, gi t 2.

rofnD gi t < 2D y (t) = 0F idte oti kpoie forD den eni prithto n diqwrzoume periptsei me sqmt gi th sunlixhD n ki siwphl knme kti ttoio me mjhmtikF cEA ipieiste esei @exskhsh3A ti knont th grfik lsh th sunlixh pou qete dei sto mjhm ki sto frontistrioD pirnoume to dio potlesmF wporeite n dete D tloD oti to potlesm m eini to dio me ut pou m dwse h conv() @h 2t 4 pernei p t shme (2, 0), (10, 16) pou pern ki h euje tou sqmto TAF

Kleinontac

rofn h prpnw didiks sqediashc shmtwn mporei ekol n epektje gi smt dikrito qrnouF e uthn thn periptwshD o xon tou qrnou perilmnei mno krie timD en h sunrthsh conv() qrhsimopoieti qwr klimkwsh me ts @llwste to ts st dikrito qrnou smt eni pnt mondAF qi prdeigmD o prktw kdik sqedizei to sunhmtono tou prdeigmto 1X

nx = -5:5; x = cos(pi * nx/ 2); stem(nx, x); xlabel(Time (samples)); ylabel(Amplitude); title(Signal of Example 1); legend(Discrete Cosine);

% % % % % % %

Discrete time vector for x[n] (step = 1 sample) x[n] Plot the result (stem is for discrete time signals) Make plot pretty :-) Make plot pretty :-) Make plot pretty :-) Make plot pretty :-)

rxte toD dete to potlesmD sqediste to sto qrtD ki epieiste oti eni swsto3 cEA

You might also like