0% found this document useful (0 votes)
17 views7 pages

Program To Perform The Following Operations On Signals. A) Signal Addition, B) Signal Multiplication, C) Scaling, D) Shifting, E) Folding

Uploaded by

preamgowda756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Program To Perform The Following Operations On Signals. A) Signal Addition, B) Signal Multiplication, C) Scaling, D) Shifting, E) Folding

Uploaded by

preamgowda756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program to perform the following operations on signals.

a) Signal addition, b) Signal multiplication,


c)Scaling, d) Shifting, e)Folding
n = -10:10;
x1 = sin(0.2*pi*n);
x2 = cos(0.2*pi*n);
signal_addition = x1 + x2;
signal_multiplication = x1 .* x2;
scaling_factor = 2;
scaled_signal = scaling_factor * x1;
shift_amount = 3;
shifted_signal = circshift(x1, shift_amount);
folded_signal = fliplr(x1);
figure;
subplot(3,2,1);
stem(n, x1, 'filled');
title('Original Signal x1');
xlabel('n');
ylabel('x1[n]');
subplot(3,2,2);
stem(n, x2, 'filled');
title('Original Signal x2');
xlabel('n');
ylabel('x2[n]');
subplot(3,2,3);
stem(n, signal_addition, 'filled');
title('Signal Addition: x1[n] + x2[n]');
xlabel('n');
ylabel('x1[n] + x2[n]');
subplot(3,2,4);
stem(n, signal_multiplication, 'filled');
title('Signal Multiplication: x1[n] * x2[n]');
xlabel('n');
ylabel('x1[n] * x2[n]');
subplot(3,2,5);
stem(n, scaled_signal, 'filled');
title(['Scaled Signal: ' num2str(scaling_factor) ' * x1[n]']);
xlabel('n');
ylabel([num2str(scaling_factor) ' * x1[n]']);
figure;
subplot(2,2,1);
stem(n, shifted_signal, 'filled');
title(['Shifted Signal: x1[n-' num2str(shift_amount) ']']);
xlabel('n');
ylabel(['x1[n-' num2str(shift_amount) ']']);
subplot(2,2,2);
stem(n, folded_signal, 'filled');
title('Folded Signal: x1[-n]');
xlabel('n');
ylabel('x1[-n]');
Program to perform convolution of two given sequences
x1 = [1, 2, 3, 4]
x2 = [1, -1, 2]
N1 = length(x1);
N2 = length(x2);
N = N1 + N2 - 1;
x1_padded = [x1, zeros(1, N2-1)]
x2_padded = [x2, zeros(1, N1-1)]
y = zeros(1, N);
for n = 1:N
for k = 1:N1
if (n-k+1 > 0 && n-k+1 <= N2)
y(n) = y(n) + x1_padded(k) * x2_padded(n-k+1);
end
end
end
4)disp(y)
n_result = 0:N-1;
figure;
subplot(3,1,1);
stem(0:N1-1, x1, 'filled');
title('Sequence x1');
xlabel('n');
ylabel('x1[n]');
subplot(3,1,2);
stem(0:N2-1, x2, 'filled');
title('Sequence x2');
xlabel('n');
ylabel('x2[n]');
subplot(3,1,3);
stem(n_result, y, 'filled');
title('Convolution of x1 and x2');
xlabel('n');
ylabel('y[n]');
a = [1 -0.9];
b = 1;
[H, z] = tf2zpk(b, a);
zplane(b, a);
title('Pole-Zero Plot of H(z)');
xlabel('Real Part');
ylabel('Imaginary Part');
[H_ejw, w] = freqz(b, a, 'whole');
subplot(2,1,1);
plot(w/pi, abs(H_ejw));
title('Magnitude Response |H(e^{j\omega})|');
xlabel('\omega / \pi');
ylabel('|H(e^{j\omega})|');
subplot(2,1,2);
plot(w/pi, angle(H_ejw));
title('Phase Response ?H(e^{j\omega})');
xlabel('\omega / \pi');
ylabel('Phase (radians)');
n = 0:20;
h = filter(b, a, [1 zeros(1, length(n)-1)]);
figure;
stem(n, h, 'filled');
title('Impulse Response h(n)');
xlabel('n');
ylabel('h[n]');
5Computation of N point DFT of a given sequence
x = [1, 2, 3, 4];
N = length(x);
x = [x, zeros(1, N-length(x))];
X = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1) * exp(-1i * 2 * pi * k * n / N);
end
end
disp('the N poiny DFT is:')
disp(X)
k = 0:N-1;
magnitude_spectrum = abs(X);
phase_spectrum = angle(X);
figure;
subplot(2,1,1);
stem(k, magnitude_spectrum, 'filled');
title('Magnitude Spectrum');
xlabel('Frequency Index k');
ylabel('|X(k)|');
subplot(2,1,2);
stem(k, phase_spectrum, 'filled');
title('Phase Spectrum');
xlabel('Frequency Index k');
ylabel('?X(k) (radians)');

8Develop decimation in time radix-2 FFT algorithm


x = [1, 2, 3, 4, 5, 6, 7, 8];

X = dit_radix2_fft(x);

disp('Input sequence:');
disp(x);
function X = dit_radix2_fft(x)
N = length(x);
if N == 1
X = x;
return;
end
if mod(N, 2) ~= 0
error('The length of the input sequence must be a power of 2.');
end
x_even = x(1:2:N);
x_odd = x(2:2:N);
X_even = dit_radix2_fft(x_even);
X_odd = dit_radix2_fft(x_odd);
X = zeros(1, N);
for k = 1:N/2
twiddle_factor = exp(-2j * pi * (k-1) / N);
X(k) = X_even(k) + twiddle_factor * X_odd(k);
X(k + N/2) = X_even(k) - twiddle_factor * X_odd(k);
end
end
6 Using the DFT and IDFT, compute the following
x1 = [1, 2, 3, 4];
x2 = [1, 2, 1, 2];
N1 = length(x1);
N2 = length(x2);
N = max(N1, N2);
x1 = [x1, zeros(1, N-N1)];
x2 = [x2, zeros(1, N-N2)];
X1 = zeros(1, N);
X2 = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X1(k+1) = X1(k+1) + x1(n+1) * exp(-1i * 2 * pi * k * n / N);
X2(k+1) = X2(k+1) + x2(n+1) * exp(-1i * 2 * pi * k * n / N);
end
end
disp('The DFT of X1 is :')
disp('The DFT of X2 is :')
disp(X1)
disp(X2)
Y_circular = X1 .* X2;
y_circular = zeros(1, N);
for n = 0:N-1
for k = 0:N-1
y_circular(n+1) = y_circular(n+1) + Y_circular(k+1) * exp(1i * 2 * pi * k * n /
N);
end
end
y_circular = real(y_circular) / N;
N_linear = N1 + N2 - 1;
x1_padded = [x1, zeros(1, N_linear-N)];
x2_padded = [x2, zeros(1, N_linear-N)];
X1_padded = zeros(1, N_linear);
X2_padded = zeros(1, N_linear);
for k = 0:N_linear-1
for n = 0:N_linear-1
X1_padded(k+1) = X1_padded(k+1) + x1_padded(n+1) * exp(-1i * 2 * pi * k * n /
N_linear);
X2_padded(k+1) = X2_padded(k+1) + x2_padded(n+1) * exp(-1i * 2 * pi * k * n /
N_linear);
Y_linear = X1_padded .* X2_padded;
y_linear = zeros(1, N_linear);
for n = 0:N_linear-1
for k = 0:N_linear-1 end end
y_linear(n+1) = y_linear(n+1) + Y_linear(k+1) * exp(1i * 2 * pi * k * n /
N_linear);
y_linear = real(round(y_linear)) / N_linear;
figure; end end
subplot(2, 1, 1);
stem(0:N-1, y_circular, 'filled');
title('Circular Convolution using DFT and IDFT');
xlabel('n');
ylabel('y_{circular}[n]');
subplot(2, 1, 2);
stem(0:N_linear-1, y_linear, 'filled');
title('Linear Convolution using DFT and IDFT');
xlabel('n');
ylabel('y_{linear}[n]');
9Design and implementation of digital low pass FIR filter using a window
fs = 1000;
fc = 100;
N = 50;
wc = 2 * pi * fc / fs;
n = 0:N;
hd = (wc/pi) * sinc(wc * (n - N/2) / pi);
h = hd .* hamming(N+1)';
disp('FIR Filter Coefficients:');
disp(h);
[H, f] = freqz(h, 1, 1024, fs);
figure;
plot(f, abs(H));
title('Magnitude Response of the FIR Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
figure;
stem(n, h);
title('Impulse Response of the FIR Low-Pass Filter');
xlabel('Sample index (n)');
ylabel('Amplitude');
grid on;
t = 0:1/fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t);
y = filter(h, 1, x);
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After FIR Low-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
10 Design and implementation of digital high pass FIR filter using a window
fs = 1000;
fc = 100;
N = 50;
window_type = 'hamming';
h=highpass_fir_filter(fs, fc, N, window_type);
disp('High-Pass FIR Filter Coefficients:');
disp(h);
[H, f] = freqz(h, 1, 1024, fs);
figure;
plot(f, abs(H));
title('Magnitude Response of the FIR High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
figure;
stem(0:N, h);
title('Impulse Response of the FIR High-Pass Filter');
xlabel('Sample index (n)');
ylabel('Amplitude');
grid on;
t = 0:1/fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t);
y = filter(h, 1, x);
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After FIR High-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
11 Design and implementation of digital IIR Butterworth low pass
fs = 1000;
fc = 100;
order = 4;
Wn = 2 * fc / fs;
[b, a] = butter(order, Wn, 'low');
disp('Butterworth Filter Coefficients:');
disp('Numerator coefficients (b):');
disp(b);
disp('Denominator coefficients (a):');
disp(a);
[H, f] = freqz(b, a, 1024, fs);
figure;
plot(f, abs(H));
title('Magnitude Response of the Butterworth Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
figure;
plot(f, angle(H));
title('Phase Response of the Butterworth Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
t = 0:1/fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t);
y = filter(b, a, x);
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After Butterworth Low-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

You might also like