🎛️ MATLAB DSP Programs Collection

Complete Digital Signal Processing Programs with Code, Input & Output Examples

01 First & Second Order System Impulse Response

📝 MATLAB Code

clc;
close all;
clear all;
n = 0:10;
b = [2 0 0];
a = [1 -0.9 0];
y = dimpulse(b, a, length(n));
subplot(2,1,1);
stem(n, y);
xlabel('n --->');
ylabel('Amplitude');
title('Impulse response of first order system');
b = [1 0 0];
a = [1 0.6 0.8];
y1 = dimpulse(b, a, length(n));
subplot(2,1,2);
stem(n, y1);
xlabel('n --->');
ylabel('Amplitude');
title('Impulse response of second order system');
02 Frequency Response of System

📝 MATLAB Code

clc;
clear all;
b = input('Enter the numerator coefficients: ');
a = input('Enter the denominator coefficients: ');
[h, w] = freqz(b, a);
subplot(2,1,1);
plot(w/pi, abs(h));
grid;
xlabel('Normalized Frequency');
ylabel('Magnitude');
title('Magnitude Response');
subplot(2,1,2);
plot(w/pi, angle(h));
grid;
xlabel('Normalized Frequency');
ylabel('Phase in radians');
title('Phase Response');

📥 Expected Input:

Enter the numerator coefficients: [1 0.5]
Enter the denominator coefficients: [1 -0.5 0.2]

📤 Expected Output:

[Displays magnitude and phase response plots of the system]
03 Linear Convolution

📝 MATLAB Code

clc;
clear all;
close all;
x1 = input('Enter the first sequence x1(n) = ');
x2 = input('Enter the second sequence x2(n) = ');
yn = conv(x1, x2);
disp('The values of y(n) from linear convolution are:');
disp(yn);
n1 = 0:length(x1)-1;
subplot(3,1,1);
stem(n1, x1);
grid on;
xlabel('n1 --->');
ylabel('Amplitude');
title('First Sequence x1(n)');
n2 = 0:length(x2)-1;
subplot(3,1,2);
stem(n2, x2);
grid on;
xlabel('n2 --->');
ylabel('Amplitude');
title('Second Sequence x2(n)');

📥 Expected Input:

Enter the first sequence x1(n) = [1 2 3]
Enter the second sequence x2(n) = [1 1 1]

📤 Expected Output:

The values of y(n) from linear convolution are:
     1     3     6     5     3
[Displays plots of both input sequences]
XX Circular Convolution

📝 MATLAB Code

clc;
clear all;
close all;

x1 = input('Enter the first sequence x1(n) = ');
x2 = input('Enter the second sequence x2(n) = ');

N = length(x1);  % Assume x1 and x2 are of equal length
yn = cconv(x1, x2, N);

disp('The values of y(n) from circular convolution are:');
disp(yn);

n1 = 0:N-1;
subplot(3,1,1);
stem(n1, x1);
grid on;
xlabel('n1 --->');
ylabel('Amplitude');
title('First Sequence x1(n)');

subplot(3,1,2);
stem(n1, x2);
grid on;
xlabel('n2 --->');
ylabel('Amplitude');
title('Second Sequence x2(n)');

subplot(3,1,3);
stem(n1, yn);
grid on;
xlabel('n --->');
ylabel('Amplitude');
title('Circular Convolution y(n)');

📥 Expected Input:

Enter the first sequence x1(n) = [1 2 3]
Enter the second sequence x2(n) = [4 5 6]

📤 Expected Output:

The values of y(n) from circular convolution are:
[32 31 28]

Displays three subplots:
- First input sequence x1(n)
- Second input sequence x2(n)
- Circular convolution output y(n)
05 DFT and IDFT

📝 MATLAB Code

clc;
clear all;
close all;
xn = input('Enter the sequence x(n): ');
ln = length(xn);
xk = zeros(1, ln);
ixk = zeros(1, ln);
for k = 0:ln-1
  for n = 0:ln-1
    xk(k+1) = xk(k+1) + xn(n+1) * exp(-1i * 2 * pi * k * n / ln);
  end
end
t = 0:ln-1;
subplot(2,2,1);
stem(t, xn);
title('Input Sequence x(n)');
grid on;
subplot(2,2,2);
stem(t, abs(xk));
title('Magnitude Spectrum |X(k)|');
grid on;
subplot(2,2,3);
stem(t, angle(xk));
title('Phase Spectrum ∠X(k)');
grid on;
for n = 0:ln-1
  for k = 0:ln-1
    ixk(n+1) = ixk(n+1) + xk(k+1) * exp(1i * 2 * pi * k * n / ln);
  end
end
ixk = ixk / ln;
subplot(2,2,4);
stem(t, real(ixk));
title('Reconstructed Sequence (IDFT)');
grid on;

📥 Expected Input:

Enter the sequence x(n): [1 2 3 4]

📤 Expected Output:

[Displays 4 subplots:
- Input sequence
- Magnitude spectrum
- Phase spectrum  
- Reconstructed sequence via IDFT]
06 FFT and IFFT

📝 MATLAB Code

clc;
clear all;
close all;
x = input('Enter the sequence : ');
N = length(x);
xK = fft(x, N);
xn = ifft(xK);
n = 0:N-1;
subplot(2,2,1);
stem(n, x);
xlabel('n');
ylabel('amplitude');
title('Input Sequence');
subplot(2,2,2);
stem(n, abs(xK));
xlabel('n');
ylabel('magnitude');
title('Magnitude Response');
subplot(2,2,3);
stem(n, angle(xK));
xlabel('n');
ylabel('phase');
title('Phase Response');
subplot(2,2,4);
stem(n, xn);
xlabel('n');
ylabel('amplitude');
title('IFFT');

📥 Expected Input:

Enter the sequence: [1 2 3 4 5]

📤 Expected Output:

xK = 15.0000, -2.5000 + 3.4410i, -2.5000 + 0.8123i, -2.5000 - 0.8123i, -2.5000 - 3.4410i
xn = 1 2 3 4 5
[Displays 4 subplots showing input sequence, magnitude response, phase response, and IFFT]
13 Power Spectral Density using pwelch

📝 MATLAB Code

clc;
clear all;
close all;

N = 1024;
fs = 8000;
f = input('Enter the frequency [1 to 5000]: ');
n = 0:N-1;

x = sin(2*pi*f/fs * n);

[pxx, f_axis] = pwelch(x, [], [], N, fs);

plot(f_axis, 10*log10(pxx));
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
title('Power Spectrum using pwelch');
grid on;

📥 Expected Input:

Enter the frequency [1 to 5000]: 1000

📤 Expected Output:

[Displays a power spectrum plot with a peak around 1000 Hz]
08 IIR Filter Design

📝 MATLAB Code

clc;
clear all;
close all;
disp('Enter the IIR filter design specifications');
rp = input('Enter the passband ripple (in dB): ');
rs = input('Enter the stopband ripple (in dB): ');
wp = input('Enter the passband frequency (Hz): ');
ws = input('Enter the stopband frequency (Hz): ');
fs = input('Enter the sampling frequency (Hz): ');
w1 = 2 * pi * wp;
w2 = 2 * pi * ws;
[n, wn] = buttord(w1, w2, rp, rs, 's');
disp(['Calculated filter order: ', num2str(n)]);
[b, a] = butter(n, wn, 'low', 's');
w = linspace(0, 2*w2, 1000);
[h, om] = freqs(b, a, w);
m = 20*log10(abs(h));
an = angle(h);
figure;
subplot(2,1,1);
plot(om/(2*pi), m, 'b');
grid on;
xlabel('Frequency (Hz)');
ylabel('Gain (dB)');
title('Magnitude Response of IIR Low-Pass Filter');
subplot(2,1,2);
plot(om/(2*pi), an, 'r');
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase Response of IIR Low-Pass Filter');

📥 Expected Input:

Enter the passband ripple (in dB): 3
Enter the stopband ripple (in dB): 40
Enter the passband frequency (Hz): 1000
Enter the stopband frequency (Hz): 1500
Enter the sampling frequency (Hz): 8000

📤 Expected Output:

Calculated filter order: 6
[Displays magnitude and phase response plots]
09 FIR Low-Pass Filter

📝 MATLAB Code

clc;
clear all;
close all;
n = 20;
fp = 200;
fs = 1000;
fn = 2 * fp / fs;
window = blackman(n + 1);
b = fir1(n, fn, window);
[H, W] = freqz(b, 1, 128);
subplot(2,1,1);
plot(W/pi, abs(H));
title('Magnitude Response of FIR LPF');
ylabel('Gain');
xlabel('Normalized Frequency (\times\pi rad/sample)');
grid on;
subplot(2,1,2);
plot(W/pi, angle(H));
title('Phase Response of FIR LPF');
ylabel('Phase (radians)');
xlabel('Normalized Frequency (\times\pi rad/sample)');
grid on;

📥 Expected Input:

No user input required (parameters are predefined)

📤 Expected Output:

[Displays FIR Low-Pass Filter magnitude and phase response plots]
10 FIR High-Pass Filter

📝 MATLAB Code

clc;
clear all;
close all;
n = 20;
fp = 300;
fs = 1000;
fn = 2 * fp / fs;
window = blackman(n + 1);
b = fir1(n, fn, 'high', window);
[H, W] = freqz(b, 1, 128);
subplot(2,1,1);
plot(W/pi, abs(H));
title('Magnitude Response of FIR HPF');
ylabel('Gain');
xlabel('Normalized Frequency (\times\pi rad/sample)');
grid on;
subplot(2,1,2);
plot(W/pi, angle(H));
title('Phase Response of FIR HPF');
ylabel('Phase (radians)');
xlabel('Normalized Frequency (\times\pi rad/sample)');
grid on;

📥 Expected Input:

No user input required (parameters are predefined)

📤 Expected Output:

[Displays FIR High-Pass Filter magnitude and phase response plots]
11 Down Sampling

📝 MATLAB Code

clc;
clear all;
close all;
D = input('enter the downsampling factor');
L = input('enter the length of the input signal');
f1 = input('enter the frequency of first sinusodal');
f2 = input('enter the frequency of second sinusodal');
n = 0:L-1;
x = sin(2*pi*f1*n) + sin(2*pi*f2*n);
y = decimate(x, D, 'fir');
subplot(2,1,1);
stem(n, x(1:L));
title('Input Sequence');
xlabel('time(n)');
ylabel('amplitude');
subplot(2,1,2);
m = 0:(L/D)-1;
stem(m, y(1:L/D));
title('Decimated Sequence');
xlabel('time(n)');
ylabel('amplitude');

📥 Expected Input:

enter the downsampling factor = 5
enter the length of the input signal = 100
enter the frequency of first sinusoidal = 0.01
enter the frequency of second sinusoidal = 0.03

📤 Expected Output:

[Displays two subplots:
- Input sequence (original signal)
- Decimated sequence (downsampled signal)]
12 Up Sampling

📝 MATLAB Code

clc; clear all; close all;
L = input('enter the upsampling factor: ');
N = input('enter the length of the input signal: ');
f1 = input('enter the frequency of first sinusoidal: ');
f2 = input('enter the frequency of second sinusoidal: ');
n = 0:N-1;
x = sin(2*pi*f1*n) + sin(2*pi*f2*n);
y = interp(x, L);

subplot(2,1,1);
stem(n, x(1:N));
title('Input Sequence');
xlabel('time(n)');
ylabel('amplitude');

subplot(2,1,2);
m = 0:N*L-1;
stem(m, y(1:N*L));
title('Output (Upsampled) Sequence');
xlabel('time(n)');
ylabel('amplitude');

📥 Expected Input:

enter the upsampling factor: 5
enter the length of the input signal: 9
enter the frequency of first sinusoidal: 0.1
enter the frequency of second sinusoidal: 0.3

📤 Expected Output:

[Displays two subplots:
- Input sequence (original signal)
- Output sequence (upsampled signal with inserted zeros)]
13 DTMF Tone Generation

📝 MATLAB Code

clc; clear all; close all;
Fs = 8000;              % Sampling frequency
N = 800;                % 100ms worth of samples
t = (0:N-1)/Fs;
pit = 2*pi*t;

symbol = {'1','2','3','4','5','6','7','8','9','*','0','#'};
f = [697 697 697 770 770 770 852 852 852 941 941 941;
     1209 1336 1477 1209 1336 1477 1209 1336 1477 1209 1336 1477];

tones = zeros(N, size(f,2));
for toneChoice = 1:12
    tones(:,toneChoice) = sum(sin(f(:,toneChoice) * pit))';
    subplot(4,3,toneChoice);
    plot(t*1e3, tones(:,toneChoice));
    title(['Symbol "', symbol{toneChoice}, '": [' ...
           num2str(f(1,toneChoice)), ',', num2str(f(2,toneChoice)), ']']);
    set(gca, 'Xlim', [0 25]);
    ylabel('Amplitude');
    if toneChoice > 9
        xlabel('Time (ms)');
    end
end

set(gcf, 'Color', [1 1 1], 'Position', [1 1 1280 1024]);
annotation(gcf,'textbox', 'Position',[0.38 0.96 0.45 0.026], ...
           'EdgeColor',[1 1 1], ...
           'String', '\bf Time response of each tone of the telephone pad', ...
           'FitHeightToText','on');

📥 Expected Input:

No user input needed — tone frequencies are predefined for 12 phone symbols.

📤 Expected Output:

Displays a 4×3 subplot grid:
- Each plot shows the DTMF tone waveform for a keypad symbol
- X-axis in milliseconds (0–25 ms)
- Title with symbol and corresponding frequency pair
- An annotation at the top: "Time response of each tone of the telephone pad"