0% found this document useful (0 votes)
4 views6 pages

Saptak

The document contains a series of MATLAB scripts for a Digital Signal Processing lab assignment. It includes tasks such as generating and plotting sinusoidal waves with different sampling frequencies, adding noise to a signal, filtering the noisy signal, and analyzing a speech signal with quantization and SNR calculations. Additionally, a custom function for quantizing the speech signal is provided.

Uploaded by

Aman Gupta
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)
4 views6 pages

Saptak

The document contains a series of MATLAB scripts for a Digital Signal Processing lab assignment. It includes tasks such as generating and plotting sinusoidal waves with different sampling frequencies, adding noise to a signal, filtering the noisy signal, and analyzing a speech signal with quantization and SNR calculations. Additionally, a custom function for quantizing the speech signal is provided.

Uploaded by

Aman Gupta
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/ 6

Name: Saptak Das |Roll No: 2022ETB001

Digital Signal Processing Lab


Assignment-1

%q1
clc
A=10;%Amplitude
l=100;%length
f=20000;
fs1=1000;%Sampling frequency
p=pi/2;% phase
n=0:l-1;
phi1=2*pi*fs1/f; %Angular frequency
x1=A*cos(phi1*n+p);

figure
subplot(3,1,1);
stem(n,x1);
xlabel('Time');
ylabel("Sinusoidal wave with fs=1000Hz");

fs2=8000;%Sampling frequency
phi2=2*pi*fs2/f; %Angular frequency
x2=A*cos(phi2*n+p);

subplot(3,1,2);
stem(n,x2);
xlabel('Time');
ylabel("Sinusoidal wave with fs=8000Hz");

fs3=10000;%Sampling frequency
phi3=2*pi*fs3/f; %Angular frequency
x3=A*cos(phi3*n+p);

subplot(3,1,3);
stem(n,x3);
xlabel('Time');
ylabel("Sinusoidal wave with fs=10000Hz");
%q2
clc,clearvars
l=100;
n=0:l-
1; %original
signal
s=2*(n.*(0.9).^n);

figure
subplot(2,3,1);
stem(n,s);
xlabel('Time');
ylabel("Original signal");

%noise signal
d=rand(1,l) - 0.5;
subplot(2,3,2);
stem(n,d);
xlabel('Time');
ylabel("Noise signal");

%noise corrupted signal


x=s+d;

subplot(2,3,3);
stem(n,x);
xlabel('Time');
ylabel("Noise corrupted signal");

%filter
M = 5;
b = ones(1,M)/M;
y = filter(b,1,x);
subplot(2,3,4);
stem(n,x);
xlabel('Time');
ylabel("Filtered signal with length =5 ");

M = 7;
b = ones(1,M)/M;
y = filter(b,1,x);
subplot(2,3,5);
stem(n,x);
xlabel('Time');
ylabel("Filtered signal with length =7 ");
M = 9;
b = ones(1,M)/M;
y = filter(b,1,x);
subplot(2,3,6);
stem(n,x);
xlabel('Time');
ylabel("Filtered signal with length =9 ");

q3
clc,clearvars
n=0:1000; %inp
ut signal
u=(n>=0);
a=16;
p=sqrt(a);
x=a*u;
figure;
subplot(2,1,1);
stem(n,x);
xlabel("Time");

3
ylabel("Input signal x[n]");
i=n+1;
y=ones(size(i));
y(1)=0.5*(1+x(1));
for t=2:size(n)-1
y(t)=0.5*(p+x(t)/p);
p=y(t);
end
subplot(2,1,2);
plot(n,y);
xlabel("Time");
ylabel("Output signal y[n]");

%% q4
%Speech file read
[speech, fs]=audioread('sample-
6s.mp3'); %Speech signal plot
n=(0:length(speech)-1)/fs;
figure
plot(n,speech);
xlabel('Time');
ylabel('Amplitude');

4
title('Speech signal');

%Quantization range
minValue=min(speech);
maxValue=max(speech);

%No. of bits for quantization


bitNum=[8 ,16,32];
figure;
hold on;

for i=1:length(bitNum)
num=bitNum(i);
quantizedSpeech=quantize(speech,num,minValue,maxValue);

%SNR
snrVal=snr(speech,speech-quantizedSpeech);
plot(num,snrVal, 'o', 'DisplayName',sprintf('%d bits',num));
end

hold off;
title('SNR vs. Number of Bits/Sample');
xlabel('Number of Bits/Sample');
ylabel('SNR (dB)');
legend('show');
grid on;

5
% Function to quantize the speech signal

function quantizedSignal = quantize(signal, num, minValue,


maxValue) % Calculate the step size for quantization
stepSize = (maxValue - minValue) / (2^num - 1);

% Quantize the signal


quantizedSignal = round(signal / stepSize) * stepSize;
end

You might also like