Documente Academic
Documente Profesional
Documente Cultură
% Basic Matlab Tutorial % Tutorial 1 - Variable Definition and Plotting close all; clear all; clc; x=1:1:100; % Define a 1 Dimensional Array of Values y=x.^2; % Use of Dot Operator - Element by Element z=sqrt(x); % Square Root Operation figure(1) % Open Figure Window - Label it Figure 1 plot(x,y,'r.') % Plot X vs Y with Red Dots grid on % Turns on Grid Background xlabel('X - Axis') % X - Axis Label ylabel('Y - Axis') % y - Axis Label title('First Plot of X^2') % Chart Title % Refer to "help plot" in Matlab for more plotting options figure(2) % Open Figure Window - Label it Figure 1 plot3(x,y,z,'ko') % Plot [X,Y,Z] with Black Circles grid on % Turns on Grid Background xlabel('X - Axis') % X - Axis Label ylabel('Y - Axis') % Y - Axis Label zlabel('Z - Axis') % Z - Axis Label title('Second Plot of X^2') % Chart Title %axis([xmin xmax ymin ymax zmin zmax]); % Zoom to Limited Axis figure(3) subplot(2,1,1); plot(x,y,'r.') % Subplot(# Rows, # Cols, Select) title('Use of the Subplot command') subplot(2,1,2); plot(x,z,'b.')
Impulse FUNCTION
Step Function
function[x,n]=stepseq(n0,n1,n2) %Generates x(n)=u(n-n0);n1<=n<=n2 n=[n1:n2] x=[(n-n0)>=0] %stem(n,x) plot(n,x) xlabel('n') ylabel('x') title('Unit step sequence')
% Figure 1.13 % gives example of a continuous-time signal t=0:0.1:30; x = exp(-.1*t).*sin(2/3*t); plot(t,x) grid axis([0 30 -1 1]); ylabel('x(t)') xlabel('Time (sec)') title('Figure 1.13')
Sum of sinusoids
Exp 6
%TO Find SUM of 4 SINUSOIDAL waves of 4 different amplitudes %SINE with amplitude 2 t=0:0.001:5; A=20; x=A*sin(2*pi*t); subplot(2,2,1) plot(t,x) title('sine wave with amplitude 2') xlabel('time') ylabel('amp') %SINE with amplitude 4 t1=0:0.001:10 B=40; y=B*sin(2*pi*t1); subplot(2,2,2) plot(t1,y) title('sine with amp 4') xlabel('time') ylabel('AMP') %sine with amplitude 10 t2=0:0.001:2 C=10; z=C*sin(2*pi*t2); subplot(2,2,3) plot(t2,z) title('sine with amp 10') xlabel('time') ylabel('AMP') %sine with amplitude 0.5 t3=0:.001:20; D=0.05; W=D*sin(2*pi*t3); subplot(2,2,4) plot(t3,W) title('sine with amp 0.5') xlabel('time') ylabel('AMP') %addition of all the above sinusoids x1=padarray(x,[0,15000],0,'post'); y1=padarray(y,[0,10000],0,'post'); z1=padarray(z,[0,18000],0,'post'); v=x1-y1-z1-W; t4=0:.001:20; figure,plot(t4,v) title('sum of sines') xlabel('time') ylabel('amp')
subplot(3,1,3), plot(f,angle(Hz)) grid on title('Phase Response of H(z)') ylabel('radians*57.3=degrees')%one radian=57.3 degrees xlabel('f [Hertz]') axis([0 1000000 -2 0]); fc=443000;%carrier frequency(Hz) T=1/200;%sets span of y filter output plot %x=A1*(cos(2*pi*fc*t)+phi)(cosine wave) twopi_fc_t=(1:fs*T)*2*pi*fc/fs; A1=1; phi=0; x = A1 * cos(twopi_fc_t + phi); %you could also use a square wave or an impulse signal insteat of a cosine %wave. %=============================================== %a and b filter coefficients %=============================================== a=[1 .7];%(z-(p)) b=[.85 .85];%gain times [1 1) % =============================================== %Output y = filter(b,a,x) % =============================================== y = filter(b,a,x); %plot input x and output y %observe amplitude and phase shift of y compared to x. Will need %much lower -3dB point filter(say 200Hz) and span to see phase shift. Notice 1Vp(input) to %.7Vp(output) for -3dB point. Magnitude and phase shift at fc shown in %command window. figure(2); plot(x,'b-'); hold on; plot(y,'r-'); grid on title('input x(blue) / output y(red)') ylabel('Voltage') hold off; %Magnitude and phase at fc w=2*pi*fc/fs; z=exp(w*j); Hz=gain*(z+1)./(z-(p)); magfc=abs(Hz) phaseradfc=angle(Hz)
subplot(3,1,2); Hz(Hz==0)=10^(-8);%avoid log(0) plot(f,20*log10(abs(Hz))); grid on ylabel('dB') axis([0 13e9 -3 1]);%set -3 to -60 to zoom out subplot(3,1,3), plot(f,angle(Hz)) grid on title('Phase Response of H(z)') ylabel('radians*57.3=degrees')%one radian=57.3 degrees xlabel('f [Hertz]') axis([0 13e9 -2 2]); fcent=6500e6;%carrier frequency(Hz),notice no loss at 6500e6 %Try 6000e6 and 7000e6 to check for -3dB loss. T=1/1e6;%sets span of y filter output plot %x=A1*(cos(2*pi*fc*t)+phi)(cosine wave) twopi_fc_t=(1:fs*T)*2*pi*fcent/fs; A1=1; phi=0; x = A1 * cos(twopi_fc_t + phi); %You could also use a square wave or an impulse signal instead of a cosine %wave. %=============================================== %a and b filter coefficients %=============================================== %To get the a and b coefficients, expand the numerator and denominator. %a=(z+p)*(z-p)=z^2-(p^2)=[1 0 -(p)] a=[1 0 .7855];%(1 0 p) b=[.105 0 -.105];%gain times [1 0 -1) % =============================================== %Output y = filter(b,a,x) % =============================================== y = filter(b,a,x); %Plot input x and output y %Observe amplitude and phase shift of y compared to x. Will need %much lower center frequency bandpass filter(say 1000Hz) and span to see phase shift. %Magnitude and phase shift at fc shown in command window. figure(2); plot(x,'b-'); hold on; plot(y,'r-'); grid on title('input x(blue) / output y(red)') ylabel('Voltage') hold off; %Magnitude and phase at fcent w=2*pi*fcent/fs; z=exp(w*j); Hz=gain*(z+1).*(z-1)./(z.^2-(p)); magfcent=abs(Hz) phaseradfcent=angle(Hz)
else
fprintf('\n Sorry! You entered a wrong choice'); end den = poly(stpoles); den = real(den); H = tf(1,den) % Transfer function h = impulse(1,den,50); % Impulse Response s = step(1,den,50); % Step Response t = linspace(0,50,length(h)); figure plot(t,h) xlabel('Time') ylabel('Amplitude') title('Normalized Impulse Response') figure plot(t,s) xlabel('Time') ylabel('Amplitude') title('Normalized Step Response') realpoles = real(stpoles); imagpoles = imag(stpoles); figure plot(realpoles,imagpoles,'*') axis([-1 1 -1.5 1.5]) grid on xlabel('Sigma') ylabel('j * Omega') title('Poles of the filter in S-plane') clear
EXP 10
%DESIGN A DIGITAL FIR LOW PASS FILTER WITH THE FOLLOWING SPECIFICATIONS %WP=0.2PI %Rp=0.25dB %Ws=0.3PI %As=50dB %CHOOSE THE APPROPRIATE WINDOW FUCTION %DETERMINE THE IMPULSE RESPONSE AND PROVIDE A PLOT FOR FREQUENCY RESPONSE %OF THE DESIGNATED RESPONSE %USING HAMMING WINDOW wp=0.2*pi; ws=0.3*pi; tr_width=ws-wp; M=ceil(6.6*pi/tr_width)+1; n=[0:1:M-1]; wc=(ws+wp)/2; %IDEAL LPF CUTOFF FREQUENCY alpha=(M-1)/2; m=n-alpha+eps; hd=sin(wc*m)./(pi*m); w_ham=(hamming(M)); h=hd' .* w_ham; [H,w]=freqz(h,1,1000,'whole'); H=(H(1:1:501))'; w=(w(1:1:501))'; mag=abs(H); db=20*log10((mag+eps)/max(mag)); pha=angle(H); grd=grpdelay(h,1,w); delta_w=2*pi/1000; Rp=-(min(db(1:1:wp/delta_w+1))); %ACTUAL PASS BAND RIPPLE As=-round(max(db(ws/delta_w+1:1:501))); %MIN STOP BAND ATTENUATION %PLOTS subplot(1,1,1); subplot(2,2,1);stem(n,hd);title('Ideal Impulse Response');axis([0 M-1 -0.1 0.3]);xlabel('n');ylabel('hd(n)'); subplot(2,2,2);stem(n,w_ham);title('HAMMING WINDOW');axis([0 M-1 0 1.1]);xlabel('n');ylabel('w(n)'); subplot(2,2,3);stem(n,h);title('Actual Impulse Response');axis([0 M-1 -0.1 0.3]);xlabel('n');ylabel('h(n)'); subplot(2,2,4);plot(w/pi,db);title('Magnitude Response');axis([0 1 -100 10]);xlabel('Frequency in pi units');ylabel('Decibles');
EXP 11
%ANALOG PROTOTYPE SPECIFICATION :INVERSE MAPPING FOR FREQUENCIES T=1; Omegap=wp/T; Omegas=ws/T; %SET T=1 %PROTOTYPE PASSBAND FREQUENCY %PROTOTYPE STOPBAND FREQUENCY
%ANALOG BUTTERWORTH PROTOTYPE FILTER CALCULATION [N, Wn] = BUTTORD(wp, ws, Rp, As); %BUTTERWORTH FILTER ORDER [b,a] = butter(N,Wn,'S'); [bz,az] = impinvar(b,a,10); [Ha,Wa] = freqs(b,a,512); [Hz,Wz] = freqz(bz,az,512,10); %PLOTS subplot(1,1,1); subplot(2,2,1);plot(Wa/(2*pi),20*log10(abs(Ha)));hold on;plot(Wz,20*log10(abs(Hz)));title('Magnitude Response');xlabel('|H|');ylabel('frequency'); %plot(Wa/(2*pi),20*log10(abs(Ha))); hold on; %plot(Wz,20*log10(abs(Hz)))