Wednesday, October 14, 2015

Nummer Vier

%Part 1
T=1/8000;
t=-.2:T:.2;

x=10*cos(2*pi*15*t+1.5)+4*cos(2*pi*25*t-.5)+6*cos(2*pi*30*t-1);
y=4*cos(2*pi*15*t+.8)+6*cos(2*pi*25*t+.4)+8*cos(2*pi*30*t-1.6);

plot(t,x,t,y,'-r');
legend('x','y');
xlabel('t');

z=10*cos(2*pi*15*t+.8)+4*cos(2*pi*25*t+.4)+6*cos(2*pi*30*t-1.6);

figure
plot(t,x,t,z,'-g');
legend('x','z');
xlabel('t');

%Part 2
clear all
T=.001;
t=-.5:T:.5;
x=-1.3-8.4*cos(1.5*pi*t-.45)+4.2*sin(23*pi*t+2.8)-4.8*sin(12*pi*t-1.3);
figure
plot(t,x);
title('Part 2 x(t)');

f=[0 .75 6 11.5];
A=[-1.3 8.4 4.8 4.2];
Ph=[0 -.45+pi -1.3+pi/2 2.8-pi/2];
figure
stem(f,A);
title('Part 2 single sided Amplitude');
figure
stem(f,Ph);
title('Part 2 single sided Phase');

clear all
f=[-11.5 -6 -.75 0 .75 6 11.5];
A=[2.1 2.4 4.2 -1.3 4.2 2.4 2.1];
Ph=[-(2.8-pi/2) -(-1.3+pi/2) -(-.45+pi) 0 -.45+pi -1.3+pi/2 2.8-pi/2];
figure
stem(f,A);
title('Part 2 double sided Amplitude');
figure
stem(f,Ph);
title('Part 2 double sided Phase');

%Part 3
clear all
T=.001;
t=-22:T:10;
x1=3.9*cos(.2*pi*t-1.5);
x2=3.75*cos(.5*pi*t-.6);
x3=.5*cos(1.2*pi*t+.2);

x_imp = zeros(size(t));
x_imp(t==0) = 1/T;
H=zeros(size(t));
H(1)=0;
H(2)=0;
for i=3:1:length(t)
    H(i)=(x_imp(i)*(.9*T^2+.2*T)-.2*T*x_imp(i-1)-.3*H(i-2)+H(i-1)*(.6+.4*T))/(.3+.4*T+.8*T^2);
end

tc=-44:T:20;
z1=T*conv(x1,H);
A_max_z1 = max(z1);
z2=T*conv(x2,H);
A_max_z2 = max(z2);
z3=T*conv(x3,H);
A_max_z3 = max(z3);

figure
plot(t,x1)
hold on
plot(tc,z1,'-r')
xlim([-10 10])
title('x1 input and output')
legend('x','z');
xlabel('t');
hold off

figure
plot(t,x2)
hold on
plot(tc,z2,'-r')
xlim([-10 10])
title('x2 input and output')
legend('x','z');
xlabel('t');
hold off

figure
plot(t,x3)
hold on
plot(tc,z3,'-r')
xlim([-10 10])
title('x3 input and output')
legend('x','z');
xlabel('t');
hold off
%b
xtotal=x1+x2+x3;
ztotal=T*conv(xtotal,H);
figure
plot(t,xtotal)
hold on
plot(tc,ztotal,'-r')
title('total input and output')
legend('x','z');
xlabel('t');
hold off
%c
fi=[-.6 -.25 -.1 .1 .25 .6];
Ai=[.25 1.875 1.9 1.9 1.875 .25];
figure
stem(fi,Ai)
title('Input Amplitude Spectrum')

fo=fi;
Ao=[A_max_z3/2 A_max_z2/2 A_max_z1/2 A_max_z1/2 A_max_z2/2 A_max_z3/2];
figure
stem(fo,Ao)
title('Output Amplitude Spectrum')
%d
%For the input the significant bandwidth is .5 since the ys are all
%above our significance factor of .2

%For the output the significant bandwidth is .15 since y of the
%higher band is less than .2

Nummer Drei

%Part 1
T=.05;
t=-4:T:4;
tl=-8:T:8;
x= -((t+.25).*(US(t+2)-US(t-1)));
y= 2*exp(-0.4*(t+.5)).*cos(.5*pi*t-(pi/4)).*(US(t+.5)-US(t-2.5));
z= T*conv(x,y);
plot(t,x)
hold on
plot(t,y,'-r')
plot(tl,z,'-g')
title('Part 1 convolution')
hold off
%questions
%-8 to 8
%x from -2.05 to 1.1,y from -.55 to 2.5
%z from -2.55 to 3.45
%You don't really notice T smaller
%but it becomes blocky with larger Ts.

%Part 2
clear all
T=.01;
t=-8:T:8;
tl=-16:T:16;
x=exp(-.5*t).*US(t);
y=2*exp(.75*t).*US(-t);
z= T*conv(x,y);
figure
plot(t,x)
hold on
plot(t,y,'-r')
title('Part 2 x and y')
hold off
figure
zintegral=1.6.*exp(.75*tl).*(tl<0)+1.6.*exp(-.5*tl).*(tl>=0);
plot(tl,z)
hold on
plot(tl,zintegral,'-r')
title('Part 2 z and zintegral')
hold off

%repeat for different segments

clear all
T=.01;
t=-6:T:6;
tl=-12:T:12;
x=exp(-.5*t).*US(t);
y=2*exp(.75*t).*US(-t);
z= T*conv(x,y);
figure
plot(t,x)
hold on
plot(t,y,'-r')
title('Part 2 x and y smaller segment')
hold off
figure
zintegral=1.6.*exp(.75*tl).*(tl<0)+1.6.*exp(-.5*tl).*(tl>=0);
plot(tl,z)
hold on
plot(tl,zintegral,'-r')
title('Part 2 z and zintegral smaller segment')
hold off
%interval -4 to 4
clear all
T=.01;
t=-4:T:4;
tl=-8:T:8;
x=exp(-.5*t).*US(t);
y=2*exp(.75*t).*US(-t);
z= T*conv(x,y);
figure
plot(t,x)
hold on
plot(t,y,'-r')
title('Part 2 x and y smaller segment')
hold off
figure
zintegral=1.6.*exp(.75*tl).*(tl<0)+1.6.*exp(-.5*tl).*(tl>=0);
plot(tl,z)
hold on
plot(tl,zintegral,'-r')
title('Part 2 z and zintegral smaller segment')
hold off
%interval -2 to 2
clear all
T=.01;
t=-2:T:2;
tl=-4:T:4;
x=exp(-.5*t).*US(t);
y=2*exp(.75*t).*US(-t);
z= T*conv(x,y);
figure
plot(t,x)
hold on
plot(t,y,'-r')
title('Part 2 x and y smallest segment')
hold off
figure
zintegral=1.6.*exp(.75*tl).*(tl<0)+1.6.*exp(-.5*tl).*(tl>=0);
plot(tl,z)
hold on
plot(tl,zintegral,'-r')
title('Part 2 z and zintegral smallest segment')
hold off
%As you decrease the interval the approximated z
%gets farther off from the actual z from the integral.

%Part 3
clear all
T=.005;
t=-2:T:3;
tl=-4:T:6;
x= 3*US(((t-.0875)/1.75)+(1/2))-3*US(((t-.0875)/1.75)-(1/2));
y= 1.5*sin(4*pi*t);
z=T*conv(x,y);
ztrue=(-2.25/pi)*cos(4*pi*tl);
figure
plot(tl,z)
hold on
plot(tl,ztrue,'-r')
title('Part 3 z and ztrue')
hold off
figure
plot(t,x)
hold on
plot(t,y,'-r')
title('Part 3 x and y')
hold off
%In Part 3 aside from being shifted somewhat
%the true z seems to continue beyond the range
%of our approximation which is nonzero from about -3 to 4

%Part 4
clear all
figure
T=0.01;
t=-1:T:12;
x_imp = zeros(size(t));
x_imp(t==0) = 1/T;
H=zeros(size(t));
H(1)=0;
H(2)=0;
for i=3:1:length(t)
    H(i)=(x_imp(i)*(.9*T^2+.2*T)-.2*T*x_imp(i-1)-.3*H(i-2)+H(i-1)*(.6+.4*T))/(.3+.4*T+.8*T^2);
end
plot(t,H);
title('Part 4 Impulse response');
xlim([-2 12])
figure
tl=-2:T:24;
x=2-cos(.2*pi*t)+.25*cos(2*pi*t);
plot(t,x);
hold on
z=T*conv(x,H);
plot(tl,z,'-r');
xlabel('t');
xlim([-2 12])
ylabel('magnitude');
title('Part 4 Input and Output');
hold off

Nummer Zwei

%problem 1
clc
t=-.1:.001:.1;
i1=2.4*cos(15*pi*t-0.8);
i2=4.2*cos(15*pi*t-1.9);
i3=i1+i2;
plot(t,i1);
hold on
plot(t,i2,'-r');
plot(t,i3,'-g');
xlabel('t');
ylabel('current');
title('Current vs Time');
legend('i1','i2','i3')
hold off

%problem 2
clear all
figure
t=-10:1:10;
x=2*US(t+2.5)+2*UR(t+2)-10*US(t+1)-UR(t)...
 -UR(t-1)+US(t-1)+.5*UR(t-2.5)-.5*UR(t-6.5);

plot(t,x)
xlabel('t');
ylabel('x(t)');
title('Signal Plot');

%problem 3
clear all
figure
T=.000125;
t=0.6:T:1.6;
x=t.*(US(t-0.8)-US(t-1.1));
R=2000;
C=.00001;
y=zeros(size(t));
y(1)=0;
for i=2:1:size(t,2) %length(t)
    y(i)=(T*x(i)+R*C*y(i-1))/(R*C+T);
end
plot(t,x,'-r')
hold on
plot(t,y)
xlabel('t');
ylabel('voltage');
title('Low Pass Filter (derivative approx)');
hold off

%problem 4
clear all
figure
T=.000125;
t=0.6:T:1.6;
x=t.*(US(t-0.8)-US(t-1.1));
R=2000;
C=.00001;
y=zeros(size(t));
y(1)=0;
for i=2:1:length(t)
    y(i)=(x(i)+x(i-1)-(1-(T*R*C)/2)*y(i-1))/(1+(T*R*C)/2);
end
plot(t,x,'-r')
hold on
plot(t,y)
xlabel('t');
ylabel('voltage');
title('Low Pass Filter (integral approx)');
hold off

%problem 5
clear all
figure
R=2000;
C=.00001;
T=.000125;
t=-.1:T:.2;
x_imp = zeros(size(t));
x_imp(t==0) = 1/T;
h_actual=(1/(R*C))*exp(-(t/(R*C))).*US(t);
h_approx=zeros(size(t));
h_approx(1)=0;
for i=2:1:length(t)
    h_approx(i)= (T*x_imp(i)+R*C*h_approx(i-1))/(R*C+T);
end
plot(t,h_approx,'-r')
hold on
plot(t,h_actual)
xlabel('t');
ylabel('magnitude');
title('Impulse Responses');
legend('h approx','h actual')
hold off

%problem 6
clear all
figure
T=0.000125;
t=0:T:10;
x= -3*UR(t-1)+3*US(t-1)+3*US(t-2)+3*US(t-3)+3*UR(t-4);
plot(t,x,'-r');
hold on
y=zeros(size(t));
y(1)=2;
y(2)=2-0.35*T;
for i=3:1:length(t)
    y(i)=(x(i)*(.9*T^2+.2*T)-.2*T*x(i-1)-.3*y(i-2)+y(i-1)*(.6+.4*T))/(.3+.4*T+.8*T^2);
end
plot(t,y);
xlabel('t');
ylabel('magnitude');
title('Diff EQ from Prelim');
hold off

%calculations
%i3=2.4*cos(15*pi*t-0.8)+4.2*cos(15*pi*t-1.9)

Nummer Eins

%%% Part 3
pwd
cd s:\DSPLab\Lab1

clear all

a=1; b=2; c=3; d=4;
save variables.mat
clear all
whos
load variables.mat

%%% Part 4
clear all

a=3-4j, b=real(a), c=imag(a), d=abs(a), e=angle(a)

f=4; g=9; h=sqrt(f)+j*sqrt(g)

z=5+7*j;
m=real(z), n=imag(z), p=abs(z), q=angle(z)

%%% Part 5
clear all

a=[3 4; 2 1]
b=[1.5 -2.4 3.5 0.7; -6.2 3.1 -5.5 4.1; 1.1 2.2 -0.1 0]

b(1)
e=b(2,3), f=b([2 3],[1,3]), g=b(2,[3 4])

h=[1 2 3], k=[4; 7], m=[5 6; 8 9]

n=[h; k m]

clear all

a=[3 5 9], b=[3; 5; 9]
c=2:5, d=3:2:9

x=0.5:0.25:2.0;
y=sqrt(x);
x,y

f=[10 5 4 7 9 0]
g=[2 5 6]; h=f(g)
m=[1.5 -2.4  3.5 0.7; -6.2  3.1 -5.5  4.1; 1.1 2.2 -0.1  0]
n=m(1:2,2:4), o=m(:, 1:2), p=m(2, :)

clear all
roots ([2 4 10])
clear all

x= (-4 + sqrt(4^2-4*2*10))/(2*2)
x1= (-4 - sqrt(4^2-4*2*10))/(2*2)

%%% Part 6
clear all

'Signal and System Analysis'

M='MATLAB Character String'
C=M(8:16)

c='We learn to use MATLAB in EE 266 Laboratory'
A=strcat(c(17:22),c(33:43))

%%% Part 7
clear all

a=[1 2; 3 4]; b=[3 1; 7 8]; c=[2 4];
d=a+b, e=c*a, f=a^2, g=c'
h=a\b,  k=b/a
m=a.*b, n=b./a, o=b.^a

clear all
a=[1.5 3.3; 6 -4.5; -2.5 .7]
b=[.5 .3; -.1 .2; .4 -.3]
c=[1 2; 1 2]
d=[3.1 1.4 -.3; -.5 1.6 .1]
a-b*c^2+2*d'

clear all
b=[1 2 3; 4 5 6]
c=[3 2 1; 6 5 4]
d=[9 8 7; 1 2 3]
for(ii=1:1:2)
   for(jj=1:1:3)
      b(ii,jj)-c(ii,jj)*d(ii,jj)^4
   end
end

2-2*8^4

%%% Part 9
clear all
a=[1 3 2; 4 6 5], b=a>2&a<=5

c=[1 5 3 4 7 8], d=c>4

clear all
a=[1.2 -3.2 24; .6 -.3 -.5; -2.3 1.6 20]
c=a<2&a>=-1&a~=.6
b=a.*c
b(2,2)=2*b(2,2)*(b(2,2)<0);
b(2,3)=2*b(2,3)*(b(2,3)<0);
b

%%% Part 10
clear all
t=0.1;
x=2^t*sqrt(t)-sin(2*t)/3
y=2^(t*sqrt(t))-sin(2*t)/3
f=0:2:4; w=2*pi*f;
X=(3-j*0.1*w)./(1.5+j*0.2*w)

t= 0:0.5:2;
x=(t+1).*(t>=0&t<1)+2*(t>=1&t<=2)

clear all

x=[-3.6, -2.5, -1.4, -1, 0, 1.4, 2.5, 3.6]
round(x)
floor(x)
ceil(x)

clear all

x=(log(2+(sin(3))^2)+exp(-.2))/(sqrt(2^(1.6)+3^(-.5)))

t=-1.2
while(t~=1.6)
   w=3*t^3+2*t^2-t+sin(t)
   x=0*(t<0)+2*(t>=0)
   t=t+.4;
end

%%% Part 11
clear all

t=0:0.2:0.8; x=zeros(size(t));
for k=1:3;
    x=x+sqrt(k)*t.^sqrt(1.2*k);
end;

for m=1:3;
    for n=1:4;
        y(m,n)=m+n;
    end;
end;

clear all
t=0:.5:10; y=zeros(size(t));
for q=1:length(t);
    y=t+q;
end;

clear all
n=0;
d=5;
if n==2;
    y=10*d;
else;
    y=0;
end;

clear all

n=1;
while 2*n<5000; n=2*n;end;

for k=1:4;
    if k==1; x(k)=3*k;
        else if k==2|k==4;
            x(k)=k/2;
        else;
            x(k)=2*k;
        end;
    end;
end;

clear all

c='t'; n=2;
if c=='f'; c='false'; y=Nan; end;
d=0.1:0.1:0.4;
if c=='t';
    if n==2;
       y=10*d(n);
    else;
       y=0;
    end;
end;
if n==2;
    y=10*d(n);
else;
    y=0;
end;

%number2
clear all
t=0;
f=10;
x=[0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0];
for k=1:1:5
   for m=1:1:3
       x(k,m)=3*cosd(2*pi*f*t+.1)
       f=f+5;
   end;
   f=10;
   t=t+.1;
end;

%3
clear all
t=0;
for w=35:5:45
    while(exp(1.2)*cos(w*t)<10 && t^3 <10)
        t=t+.01;
    end;
    t=t-.01
    t=0;
end;

%4
clear all
t=-1;
x=zeros(1,10001);
for k=1:1:10001
    x(k)=exp(-abs(t));
    t=t+.0002;
end;

%%% Part 12
clear all

a=[1 0 2 3 0 4];
b=find(a)
n=find(a>2)
size(a)
c=zeros(size(a))
max(a)
min(a)
mean(a)
sum(a)

d=-0.1:0.1:0.2;
dm=meshgrid(d,1:3)

%2
clear all
t=0;
for k=1:1:11
    x(k)=4*cos(2*pi*t+.2)+3*sin(pi^2*t);
    t=t+.1;
end;
max(x)
min(x)
mean(x)
find(x>4)

%3
clear all
A=[1 4 3 2; 4 1 2 5; 3 3 5 1];
size(A)
max(max(A))
min(min(A))
max(A,[],2)
min(A,[],2)
mean(A)
mean(mean(A))

%%% Part 13
clear all
t=0:0.01:10;
f1=0.2;
f2=0.425;
[s1, s2, s3]=sumsin(t,f1,f2);
plot(t,s1);
hold on
plot(t,s2,'-r');
plot(t,s3,'-g');
xlabel('t');
ylabel('y');
title('AJP Lab 1 Sinusoids');
legend('s1','s2','s3')
hold off
figure
x1=t; x2=t; x3=t;
subplot(3,1,1); plot(x1,s1);
title('AJP Lab 1 Sinusoids');
ylabel('y1');
subplot(3,1,2);plot(x2,s2);
ylabel('y2');
subplot(3,1,3);plot(x3,s3);
xlabel('t');
ylabel('y3');