MATLAB CODE LIBRARY

Your DSP Lab Codes.

All 6 experiments in one place. Find your code and copy it in one click.
جميع أكواد تجارب معالجة الإشارات الرقمية، جاهزة للنسخ بسهولة.

06 Experiments08 Code SnippetsMATLAB .m
اختر التجربة، ثم اضغط Copy Code لنسخ الكود كاملًا.
01

Experiment 1

Continuous & Discrete-Time Signals

2 Parts

الإشارات المستمرة والمتقطعة

mPart 1 — Continuous-Time Signal
%% Generation of continuous time signal
clc; clear all;
t = 0:0.0005:1;
a = 10;
f = 13;
xa = a*sin(2*pi*f*t);
subplot(2,1,1); plot(t,xa);grid
xlabel('Time, msec'); ylabel('Amplitude'); title('Continuous-time signal x_{a}(t)');;
axis([0 1 -11 11])
mPart 2 — Discrete-Time Signal
clear all;
a = 10;
f = 13;
T = 0.01;
n = 0:T:1;
xs = a*sin(2*pi*f*n);
k = 0:length(n)-1;
stem(k,xs);
grid
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete-time signal x[n]');
axis([0 (length(n)-1) -10.2 10.2])
02

Experiment 2

Elementary Sequences

1 Code

الإشارات الأساسية

mMATLAB Code
% Generations of Elementary Sequences  
clc; clear all; close all;  
t = -3:1:3;  
y = [zeros(1,3),ones(1,1),zeros(1,3)];  
subplot(2,2,1);stem(t,y);  
ylabel('Amplitude  >'); xlabel('(a)n  >'); title('Unit Impulse Signal');  

% program for generation of unit step of sequence [u(n)- u(n)-N]  
t  = -4:1:4;  
y1 = ones(1,9);  
subplot(2,2,2);stem(t,y1);  
ylabel('Amplitude  >'); xlabel('(b)n  >'); title('Unit step');  

% program for generation of ramp signal  
n1 = input('Enter the value for end of the seqeuence '); %n1 = <any value>7 %  
x = 0:n1;  
subplot(2,2,3);stem(x,x);  
ylabel('Amplitude  >'); xlabel('(c)n  >'); title('Ramp sequence');  

% program for generation of exponential signal  
n2 = input('Enter the length of exponential seqeuence ');     %n2 = <any value>7   
t  = 0:n2;  
a  = input('Enter the Amplitude');     %a=1   
y2 = exp(a*t);  
subplot(2,2,4); plot(t,y2);  
ylabel('Amplitude  >'); xlabel('(d)n  >'); title('Exponential sequence');  

disp('Unit impulse signal');y  
disp('Unit step signal'); y1 
disp('Unit Ramp signal'); x 
disp('Exponential signal');y2
03

Experiment 3

Summation & Subtraction

1 Code

جمع وطرح الإشارات

mMATLAB Code
% Summation and Subtraction Processes
clc; clear all; close all;

n1 = -2:1;
x = input('ENTER THE FIRST SEQUENCE X: ');

n2 = 0:3;
y = input('ENTER THE SECOND SEQUENCE Y: ');

subplot(2,2,1);
stem(n1,x); grid on;
xlabel('time');
ylabel('amplitude');
title('FIRST SEQUENCE');
axis([-4 4 -5 5]);

subplot(2,2,2);
stem(n2,y); grid on;
xlabel('time');
ylabel('amplitude');
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);

% Finding the duration of output signal
n3 = min(min(n1),min(n2)):max(max(n1),max(n2));

s1 = zeros(1,length(n3));
s2 = zeros(1,length(n3));

s1((n3>=min(n1)) & (n3<=max(n1))) = x;
s2((n3>=min(n2)) & (n3<=max(n2))) = y;

% Summation
summ = s1+s2;
disp('Summation Results')
disp(summ)

subplot(2,2,3);
stem(n3,summ); grid on;
xlabel('time');
ylabel('amplitude');
title('SUMMATION');
axis([-4 4 -5 5]);

% Subtraction
sub = s1-s2;
disp('Subtraction Results')
disp(sub)

subplot(2,2,4);
stem(n3,sub); grid on;
xlabel('time');
ylabel('amplitude');
title('SUBTRACTION');
axis([-4 4 -5 5]);
04

Experiment 4

Delay, Advance & Folding

1 Code

تأخير وتقديم وعكس الإشارة

mMATLAB Code
clc; clear all; close all;
x =input('ENTER THE SEQUENCE x(n):');
n1=input('Enter the amount to be delayed = ');
n2=input('Enter the amount to be advanced = ');
n = -2:2;
subplot(4,1,1); stem(n,x); grid
title('Signal x(n)');
axis([min(n)-n2 max(n)+n1 -5 5]);
% Delay
m=n+n1;
subplot(4,1,2); stem(m,x); grid
title('Delayed signal x(n-n1)');
axis([min(n)-n2 max(n)+n1 -5 5]);
% Advance
t=n-n2;
subplot(4,1,3); stem(t,x); grid
title('Advanced signal x(n+n2)');
axis([min(n)-n2 max(n)+n1 -5 5]);
%Folding or reversing:
c = fliplr(x);
y = fliplr(-n);
disp('FOLDED SEQUENCE')
disp(c)
subplot(4,1,4); stem(y,c); grid
title('Folded Signal x(-n)');
axis([min(n)-n2 max(n)+n1 -5 5]);
05

Experiment 5

Linear & Circular Convolution

1 Code

الالتفاف الخطي والدائري

mMATLAB Code
clc; clear all; close all;
x=input('enter the 1st sequence ');
h=input('enter the 2nd sequence ');
%% Linear Convolution
y = conv(x,h);
% Plot Linear Convolution
l = length(y);
figure; subplot(3,1,1); stem(x);xlim([0 l])
ylabel('Amplitude --.'); xlabel('(a) n --.'); title('first sequence');
subplot(3,1,2); stem(h);xlim([0 l])
ylabel('Amplitude --.'); xlabel('(b) n --.'); title('Second sequence');
subplot(3,1,3); stem(y); xlim([0 l])
ylabel('Amplitude --.'); xlabel('(c) n --.'); title('Convoluted sequence');
disp('The resultant signal is'); y
%% Circular Convolution
% Zero Padding Linear Convolution
nx = length(x);
nh = length(h);
% N = max(nx,nh); % N = nx+nh-1; %Choice #1
%Choice #2
N = input('enter the length of the circular CONV '); %Choice #3
%
xpd = [x zeros(1,(N-nx))];
hpd = [h zeros(1,(N-nh))];
for i = 1:N
    k = i;
    for j = 1:nh
        H(i,j)=xpd(k)*hpd(j);
        k = k-1;
        if k == 0
            k = N;
        end
    end
end
yc=zeros(1,N);
M=H';
for j = 1:N
    for i = 1:nh
        yc(j)=M(i,j)+yc(j);
    end
end
disp('The output sequence is yc(n)='); yc
%Plot Cicular Convolution
lc = length(yc);
figure;
subplot(3,1,1); stem(xpd);xlim([0 lc])
ylabel('Amplitude --.'); xlabel('(a) n --.'); title('first sequence');
subplot(3,1,2); stem(hpd);xlim([0 lc])
ylabel('Amplitude --.'); xlabel('(b) n --.'); title('Second sequence');
subplot(3,1,3); stem(yc); xlim([0 lc])
ylabel('Amplitude --.'); xlabel('(c) n --.'); title('Circular Convolution');
06

Experiment 6

Sampling Theorem & FFT

2 Parts

أخذ العينات وتحويل فورييه السريع

mPart 1 — Sampling Theorem
clc; clear all; close all;
tfinal = 0.05;
t = 0:0.00005:tfinal;
f = input('Enter the analogy frequency f = ');
xt = cos(2*pi*f*t);
%Under sampling plot
fs1 = 1.3*f;
n1 = 0:1/fs1:tfinal;
xn = cos(2*pi*f*n1);
subplot(3,1,1); plot(t,xt,'b',n1,xn,'r*-');
title('under sampling'); xlabel('time'); ylabel('amplitude');
%Nyquist plot
fs2=2*f;
n2 = 0:1/fs2:tfinal;
xn = cos(2*pi*f*n2);
subplot(3,1,2); plot(t,xt,'b',n2,xn,'r*-');
title('nyquist sampling'); xlabel('time'); ylabel('amplitude');
%Over sampling plot
fs3 = 5*f;
n3 = 0:1/fs3:tfinal;
xn = cos(2*pi*f*n3);
subplot(3,1,3); plot(t,xt,'b',n3,xn,'r*-');
title('over sampling'); xlabel('time'); ylabel('amplitude');
mPart 2 — FFT & Sampling
clc; clear all; close all;
f1 = input('Enter the first sine wave frequency f1 = ');
f2 = input('Enter the second sine wave frequency f2 = ');
fn = 2*max(f1,f2);
fs = 0.5*fn;
t = [0:1/fs:0.1];
x = cos(2*pi*f1*t)+cos(2*pi*f2*t);
xk = fft(x);
f = [0:length(xk)-1]*fs/length(xk);
figure, plot(f,abs(xk));
xlabel('frequency'); ylabel('amplitude'); title('Under Sampling');
grid;
fs = fn;
t = 0:1/fs:0.1;
x = cos(2*pi*f1*t)+cos(2*pi*f2*t);
xk = fft(x);
f = [0:length(xk)-1]*fs/length(xk);
figure; plot(f,abs(xk));
xlabel('frequency');ylabel('amplitude');title('Nyquist Rate Sampling');
grid;
fs = 5*fn;
t = 0:1/fs:0.1;
x = cos(2*pi*f1*t)+cos(2*pi*f2*t);
xk = fft(x);
f = [0:length(xk)-1]*fs/length(xk);
figure; plot(f,abs(xk));
xlabel('freq'); ylabel('Amplitude');title('Over Sampling');
grid;
Code copied successfully!