answers to selected excercises, ch 18-27 ......answers to selected excercises, ch 18-27 exercise...

17
ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2 nd order Butterworth and its output graph. To modify the script for the 115-125 band, change the values in the butter command below, i.e.: [b a]=butter(2,[115/500 125/500]); % Exercise 18.1 % Spectrum srate=1000; % sample rate pt=512; % points (2^n) for the FFT range=(pt/2); % range for the spectral plot t=0:1/srate:0.6; % time axis f=srate*(0:range)/pt; % frequency axis x=sin(2*pi*50*t)+sin(2*pi*120*t); y=x+randn(1,length(t)); % signal + noise in mV % addition to implement 2 nd order Butterworth filter [b a]=butter(2,[45/500 55/500]); y=filter(b,a,y); figure % plot signal plot(t,y) title('Time Series') xlabel('time (s)') ylabel('Amplitude (mV)') Y=fft(y,pt); % do a 512 pt FFT Pyy=Y.*conj(Y)/pt; % Power spectrum figure % Plot result plot(f,Pyy(1:length(f))); title('Powerspectrum') xlabel('Frequency (Hz)') ylabel('Power (mV2)') Note that, as compared to the unfiltered result in Fig. 7.2, the power outside the 45-55Hz band is now reduced.

Upload: others

Post on 29-Aug-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 18.1

The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd

order Butterworth and

its output graph. To modify the script for the 115-125 band, change the values in the butter

command below, i.e.: [b a]=butter(2,[115/500 125/500]);

% Exercise 18.1

% Spectrum

srate=1000; % sample rate

pt=512; % points (2^n) for the FFT

range=(pt/2); % range for the spectral plot

t=0:1/srate:0.6; % time axis

f=srate*(0:range)/pt; % frequency axis

x=sin(2*pi*50*t)+sin(2*pi*120*t);

y=x+randn(1,length(t)); % signal + noise in mV

% addition to implement 2nd order Butterworth filter

[b a]=butter(2,[45/500 55/500]);

y=filter(b,a,y);

figure % plot signal

plot(t,y)

title('Time Series')

xlabel('time (s)')

ylabel('Amplitude (mV)')

Y=fft(y,pt); % do a 512 pt FFT

Pyy=Y.*conj(Y)/pt; % Power spectrum

figure % Plot result

plot(f,Pyy(1:length(f)));

title('Powerspectrum')

xlabel('Frequency (Hz)')

ylabel('Power (mV2)')

Note that, as compared to the unfiltered result in Fig. 7.2, the power outside the 45-55Hz band is

now reduced.

Page 2: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 19.1

A general expression for the relationship is �̂�𝑛 =𝑛−1

𝑛𝑚𝑛−1 +

1

𝑛𝑥𝑛

This can be rewritten:

�̂�𝑛 =𝑛 − 1

𝑛𝑚𝑛−1 +

1

𝑛𝑥𝑛 =

𝑛 − 1

𝑛

1

(𝑛 − 1){𝑥1 + 𝑥2 + ⋯ 𝑥𝑛−1} +

1

𝑛𝑥𝑛

Cancelling 𝑛 − 1 gives

�̂�𝑛 =1

𝑛{𝑥1 + 𝑥2 + ⋯ 𝑥𝑛−1 + 𝑥𝑛}

This result can be written as:

�̂�𝑛 =1

𝑛∑ 𝑥𝑖

𝑛𝑖=1 ,

i.e. the expression for the estimated mean of 𝑛 observations of 𝑥𝑖.

Page 3: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 19.2

The equation (19.7) to compute the new variance combined form the two s1 and s2 is:

21

111

sss

With a bit of algebra, we get

𝑠 =𝑠1𝑠2

𝑠1 + 𝑠2

This will be smaller than either s1 or s2

To demonstrate this, let’s ask if the statement 𝑠1𝑠2

𝑠1+𝑠2< 𝑠1 is true.

Multiply both sides with 𝑠1 + 𝑠2, and the question is now if:

𝑠1𝑠2 < 𝑠12 + 𝑠1𝑠2, which must be true if s1 is not zero.

The same procedure can be followed to show that s is smaller than s2.

Thus for nonzero variance values s1 and s2, their combined effect (i.e. s) will result in a

reduction of the variance.

(Note that the relationship between s1 and s2 is the same as for a resulting resistance R of

two parallel resistors R1 and R2. Also in this case, adding a resistor R1 in parallel to any

other resistance R2 results in a reduction of the combined resistance R that can be

computed with: 21

111

RRR )

Page 4: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 19.4

See also Section 19.2.2

Find the best fit for �̂�𝑖= a xi by minimizing

the 𝐸𝑟𝑟𝑜𝑟 = ∑ [(�̂�𝑖 − 𝑦𝑖)2] = ∑ [(𝑎𝑥𝑖 − 𝑦𝑖)

2]𝑁𝑖=1

𝑁𝑖=1

This will be accomplished by setting its partial derivative with respect to coefficient a to

zero:

𝜕(𝐸𝑟𝑟𝑜𝑟)

𝜕𝑎= 2 ∑ [(𝑎𝑥𝑖 − 𝑦𝑖)𝑥𝑖]

𝑁

𝑖=1= 0

With a bit of algebra we get:

𝑎 ∑ 𝑥𝑖2

𝑁

𝑖=1− ∑ 𝑥𝑖𝑦𝑖

𝑁

𝑖=1= 0

Solving for a gives:

𝑎 =∑ 𝑥𝑖𝑦𝑖

𝑁𝑖=1

∑ 𝑥𝑖2𝑁

𝑖=1

Page 5: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 20.1 Hint: Review Section 20.2 and compare this with the examples in Appendix 3.4.

Page 6: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 20.3 We define the firing threshold at 0.5:

1 − exp(−𝐶𝑟𝑇) = 0.5,

where C is the convergence ratio, r is the rate of firing and T the epoch over which we observe

the response. We set T equal to one for convenience. Next we re-arrange and take the log:

log[exp(−𝐶𝑟)] = log 0.5 ≈ −0.7

Now we can determine the sensor’s firing rate required to reach the threshold at the mitral cell

level:

𝑟 ≈ 0.7/𝐶

A plot of this relationship for values between and is shown here.

This plot demonstrates that at higher levels of convergence the mitral cells may respond while the

activity at the sensory level may be ‘hardly’ observable!

Page 7: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 21.1

For the power of the scaling signal compute

124

31

24

33

24

33

24

31)(4

22224

1

2

i

iDs

For the average amplitude of the scaling signal compute

22

2

24

31

24

33

24

33

24

31)(4

4

1

i

iDs

For the power of the wavelet signal compute

124

31

24

33

24

33

24

31)(4

222224

1

i

iDw

For the average amplitude of the wavelet signal compute

024

31

24

33

24

33

24

31)(4

4

1

i

iDw

Page 8: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 22.3

The four coefficients of the Daubechies wavelet, applied to x(n-3), x(n-2), x(n-1), x(n) are:

24

31,

24

33,

24

33,

24

31

In order to analyze its effect as a filter, following the reasoning in Section 22.2, we are interested

in the reversed version of the wavelet. The transfer function for the reversed wavelet is therefore:

321

24

31

24

33

24

33

24

31

)(

)()( zzz

zX

zYzH

Applying the MATLAB freqz command for both the wavelet and scaling signals shows that the

wavelet is indeed a high pass filer and the scaling a low pass filter. Note that we use the fliplr

command to reverse the wavelet coefficients

clear;close all

b=[(1-sqrt(3))/(4*sqrt(2)) (-3+sqrt(3))/(4*sqrt(2)) (3+sqrt(3))/(4*sqrt(2))

(-1-sqrt(3))/(4*sqrt(2))];

a=[1];

freqz(fliplr(b),a)

bs=[(1+sqrt(3))/(4*sqrt(2)) (3+sqrt(3))/(4*sqrt(2)) (3-sqrt(3))/(4*sqrt(2))

(1-sqrt(3))/(4*sqrt(2))];

figure;freqz(fliplr(bs),a)

Page 9: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Result for the wavelet – a high pass characteristic

Result for the scaling – a low pass characteristic

Page 10: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 23.2

We modify pr23_4 by removing the part on initial condition and replacing it with a loop for a

range of inputs, e.g.: u=0:.2:3

This modification results in a series of responses that we can employ to determine the spikes

generated over the simulation interval. Next we count these spikes by determining maxima in the

potential v and show the I/O relationship by plotting the detected spikes against the input.

% Exercise 23.2

% Excitable Systems: 1D on a circle

clear;

close all;

% parameters

dt=.01;

I=0; % current, e.g 0 or 0.2

u=0.9;

t(1)=0;

n=10000;

figure;hold;

count=0;

for u=0:.2:3;

count=count+1;

in(count)=u;

phi(1)=pi;

for i=1:n; % loop to simulate the output

d_phi=(u+I+sin(phi(i)))*dt;

phi(i+1)=phi(i)+d_phi; % update phase (phi)

v(i)=cos(phi(i)); % Membrane Potential=projection of phi on X-axis

t(i+1)=t(i)+dt;

end;

plot(t(1:n),v,'k')

f(count)=0;

for i=2:length(v)-1;

if (v(i)>v(i-1))&(v(i)>v(i+1));

f(count)=f(count)+1;

plot(t(i),v(i),'r+')

end;

end;

end;

title('Superimposed response for different input. Zoom to check detection

(red +)')

xlabel('Time')

ylabel('Amplitude')

figure;

plot(in,f,'r')

title('Excitable system: u+sin(phi). I/O relationship')

xlabel('Input (AU)')

ylabel('Output(Spikes/s)')

Page 11: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Page 12: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 24.1 Use the third order term of the Volterra series to compute the response to )(tC :

32132132133 )()()(),,()()( dddtCtCtChtCHty

)()()()(),,( 3

3

)(

3213213213

3

3

tHCdddttthC

tH

In this case we find a scaling that is proportional with C

3 [In contrast, the scaling for a linear

system is equal to C].

Page 13: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 24.5 % Exercise 24.5

clear,clc,close all

set(0,'defaultaxesfontname','courier new')

set(0,'defaultaxesfontsize',12)

%% a) plot the UIR

n = -2:8;

h = zeros(size(n));

h(3) = 1;

h(4) = 2;

h(5) = 4;

h(6) = 2;

h(7) = 1;

h(8) = 0.5;

h(9) = 0.25;

figure,stem(n,h,'linewidth',2)

axis tight, box off, grid on

xlabel('n')

ylabel('value')

title('UIR of L component')

%% b) plot the analytic second-order Volterra kernel

[nn1, nn2] = meshgrid(n,n);

h2 = zeros(size(nn1));

for i = 1:numel(nn1)

h2(i) = h(n==nn1(i)).*h(n==nn2(i));

% given pairs of impulses at times nn1 and nn2, this is what the output

% of the squarer should look like: multiplication of the nn1 impulse

% response by the nn2 impulse response. We can ONLY compute the

% paired-pulse impulse response in this manner if we KNOW our system

% beforehand.

end

fig_h1 = figure; mesh(nn1,nn2,h2);

axis tight, box off, grid on

xlabel('n1')

ylabel('n2')

zlabel('value')

title('order 2 Volterra kernel')

ax_h1 = gca;

%% c) set up the cascade and take dev from superposition:

yy = @(xx) 0.25*xx(1:(end-6)) + 0.5*xx(2:(end-5)) + xx(3:(end-4)) +

2*xx(4:(end-3)) +...

4*xx(5:(end-2)) + 2*xx(6:(end-1)) + xx(7:end);

zz = @(yy) yy.^2;

tot = @(xx) zz(yy(xx));

Page 14: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

% remember to have a 6-zero buffer to your signal (i.e., time 0 is located

% at index 7)

% you have the following differences in delT to test:

delT = 0:7; % doesn't matter which impulse comes first

t = -6:9;

% now let's go through and test them, using deviation from superposition as

% we do not know exactly the form our system takes

fig_h2 = figure;

fig_h2 = copyobj(ax_h1,fig_h2);

hold all

for i = 1:numel(delT)

x1 = (t==0); % initial pulse

x2 = (t==delT(i)); % delayed pulse

xs = x1 + x2; % summed pulses

y1 = tot(x1); % response to just the first pulse

y2 = tot(x2); % response to just the second pulse

yt = tot(x1 + x2); % response to both pulses

% now take your deviation from superposition to get H2

H2 = (yt - y1 - y2)/2;

% now we evaluate the kernel across all lags for which delT is

% true, thus giving us the paired-impulse response to plot along the

% diagonal

tt = 0:(numel(H2)-1);

tau1 = tt( tt <= ((numel(H2)-1)-delT(i)) );

tau2 = tau1 + delT(i);

H2 = H2((numel(tt)-numel(tau1)+1):end);

% now we can plot

plot3(tau1,tau2,H2,'linewidth',2,'color',[1 0.5 1])

hold all

plot3(tau2,tau1,H2,'linewidth',2,'color',[1 0.5 1]) % symmetry!

hold all

end

axis tight, title('order 2 Volterra kernel, mesh = theoretical, pink = dev

from superpos')

ax_h2 = gca;

% restore defaults to avoid freaking out over all the monospace typeface

set(0,'defaultaxesfontname','arial')

set(0,'defaultaxesfontsize',10)

Page 15: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 25.1

Using Equation 25.7 for G1 (changing the integration variable by dropping its subscript):

dtxktxkgtxkG )()()(;)(; 11111

Using Equation 25.13 for G2:

1112

2

212121222 ),()()(),()(; dkddtxtxktxkG

If we now get to the expectation of their product, i.e. 21GG we will get two terms:

dddtxtxktxk 21212121 )()(),()()(

, and

ddktxk 11121

2 ),()()(

Both terms include an odd product of the input, and therefore both terms in the product’s

expectation will evaluate to zero (see Appendix 25.1). Hence operators G1 and G2 are

orthogonal.

Page 16: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

Exercise 27.2

a. See MATLAB script below

b. The MATLAB script below can be adapted in a straightforward manner to accomplish

task b

c. See MATLAB script below

d. See MATLAB script below

e. The logistic equation clearly shows nonlinear behavior. In general, this behavior cannot

be captured in the (linear) autocorrelation function. The return plot is a better tool to

depict the relationship within the samples of the signal generated by the logistic equation.

The chaotic regime of the equation shows strong dependence on the initial condition.

MATLAB script for Exercise 27.2 a, c, and d

% Exercise 27.2

% Logistic equation (Equation 27.1)

clear;

close all;

% parameters

n=100;

a=4; % Also test 3.25 and 2.5 (See Figure 27.1 for

% the relationship between parameter a and behavior);

p=1e-6; % small perturbation of the initial condition

% reference trajectories in matrix x1

k=0;

for ic=.1:0.05:.9;

k=k+1;

x1(k,1)=ic;

for i=2:n;

x1(k,i)=a*x1(k,i-1)*(1-x1(k,i-1));

end;

end;

% perturbed trajectories in matrix x2 (Exercise 27.2a)

k=0;

for ic=.1:0.05:.9;

k=k+1;

x2(k,1)=ic+p;

for i=2:n;

x2(k,i)=a*x2(k,i-1)*(1-x2(k,i-1));

end;

end;

figure;hold;

X=x1-x2; % compute the difference trajectories

plot(X')

['perturbation initial condition: ']

mean(X(:,1))

Page 17: ANSWERS TO SELECTED EXCERCISES, CH 18-27 ......ANSWERS TO SELECTED EXCERCISES, CH 18-27 Exercise 18.1 The modified version of pr7_1.m for the 45-55 bandpass filter, using a 2nd order

ANSWERS TO SELECTED EXCERCISES, CH 18-27

['deviation after 100 steps: ']

mean(X(:,100))

['ratio difference after 100 steps / variation initial condition: ']

ratio=mean(X(:,100))/mean(X(:,1))

title('Logistic equation - sensitivity to initial condition 1e-6')

xlabel('sample #')

ylabel('difference trajectories of ax(1-x)')

% correlation of one of trajectory 1 (Exercise 27.2c)

c=xcov(x1(1,:),'coef');

figure;

plot(c,'k')

title('Autocorrelation')

xlabel('lag')

ylabel('Correlation Coef')

% return plot of one of trajectory 1 (Exercise 27.2d)

figure;hold;

for j=2:n

plot(x1(1,j-1),x1(1,j),'k.')

end;

title('Return plot')

xlabel('x(i)')

ylabel('x(i+1)')