Mereddy Jahnavi 20011P0416 ECE-IDP V-I
ADSP LAB ASSIGNMENT-1
(1)Generate the sine wave frequency 4 KHz with the N=1000, Sampling Frequency 8000
a)Find the psd normal scale and semi log scale
b)Find its frequency spectrum by varying the fft size (fft length = 64, fft=128, fft=256)
c)Find the two sided spectrum (-fs/2 to fs/2 range) and single sided spectrum plot the spectrum (0 to fs/2 range)
d)Generate the random Noise 1000 samples
e)Generate the Gaussian noise 1000
f)Find its autocorrelation function
g)Find its psd
clc;
clear all;
close all;
%sinewave generation
fs=8000;
f=4000;
N=1000;
n=0:N;
T=1/fs;
x=sin(2*pi*(f/fs)*n);
figure;
plot(x);
title("original sine wave");
%autocorrelation
[acx,lags]=xcorr(x);
figure;
stem(lags,acx);
%psd using fft of autocorrelation
freq_scale=fs*((0:N/2))/N;
len_acx=length(acx);
fftx=fft(acx);
psd_xcorr=abs(fftx);
figure;
subplot(2,2,1);
plot(freq_scale,psd_xcorr(1:501));
title("psd using fft(xcorr) in normal scale");
subplot(2,2,2);
plot(freq_scale,10*log10(psd_xcorr(1:501)));
title("psd using fft(xcorr) in semilog");
%psd using fft
psd=abs(fft(x).^2)/N;
subplot(2,2,3);
plot(freq_scale,psd(1:501));
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
title("psd using fft normal scale");
subplot(2,2,4);
semilogy(freq_scale,psd(1:501));
title("psd using fft uding semilog");
%frequency spectrum varying fft
x_64=fft(x,64);
figure;
subplot(3,1,1);
plot(fs*(0:(64/2))/64,abs(x_64(1:33)/64));
title("64 length fft")
x_128=fft(x,128);
subplot(3,1,2);
plot(fs*(0:(128/2))/128,abs(x_128(1:65)/128));
title("128 length fft");
x_256=fft(x,256);
subplot(3,1,3);
plot(fs*(0:(256/2))/256,abs(x_256(1:129)/256));
title("256 length fft");
% 2 sided and single sided spectrum
X=fft(x);
X_origin=fftshift(X);
figure;
subplot(2,1,1);
plot(fs*((-N/2):(N/2))/N,abs(X_origin/N));
title("2 sided spectrum between -fs/2 to fs/2");
X_ss=abs(X/N);
X_ss=X_ss(1:N/2+1);
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
X_ss(2:end-1)=2*X_ss(2:end-1);
subplot(2,1,2);
plot(fs*(0:(N/2))/N,X_ss);
title("single sided spectrum between 0 to fs/2");
%random noise and gaussian noise
rn=randn(N,1);
gn=0+1*randn(N,1);
figure;
subplot(2,1,1);
plot(rn);title("random noise");
subplot(2,1,2);
plot(gn);
title("gaussian noise");
[rn_corr,lags1]=xcorr(rn);
[gn_corr,lags2]=xcorr(gn);
figure;
subplot(2,2,1);
stem(lags1,rn_corr);
title("random noise autocorrelation");
subplot(2,2,3);
stem(lags2,gn_corr);
title("gaussian noise autocorrelation");
%psd of noise
fft_rn=fft(rn_corr);
psd_rn=abs(fft_rn);
subplot(2,2,2);
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
plot(freq_scale,psd_rn(1:501));
title("psd of random noise");
fft_gn=fft(gn_corr);
psd_gn=abs(fft_gn);
subplot(2,2,4);
plot(freq_scale,psd_gn(1:501));
title("psd of guassian noise");
(2)Generate the different noise calculate mean and variance
Generate the random Noise 1000 samples
Generate the Gaussian noise 1000 samples
Generate the white noise 1000 samples
% Q2 different noise mean and variance
wn=wgn(N,1,0);
mean_wn=mean(wn);
var_wn=var(wn);
fprintf('Mean of White Noise: %.4f\n', mean_wn);
fprintf('Variance of White Noise: %.4f\n', var_wn);
mean_rn=mean(rn);
var_rn=var(rn);
fprintf('Mean of Random Noise: %.4f\n', mean_rn);
fprintf('Variance of Random Noise: %.4f\n', var_rn);
mean_gn=mean(gn);
var_gn=var(gn);
fprintf('Mean of Gaussian Noise: %.4f\n', mean_gn);
fprintf('Variance of Gaussian Noise: %.4f\n', var_gn);
(3)Generate the sine wave frequency 4 KHz with the N=1000, Sampling Frequency 8000
Added different noise to sine and plot the signal calculate the mean and SNR and variance
%sine wave with diff noise added
m=0:N-1;
y=sin(2*pi*(f/fs)*m);
x_rn=y+rn;
x_gn=y+gn;
x_wn=y+wn;
figure;
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
subplot(3,1,1);
plot(x_rn);
title("sine wave added with random noise");
subplot(3,1,2);
plot(x_gn);
title("sine wave added with gaussian noise");
subplot(3,1,3);
plot(x_wn);
title("sine wave with white noise");
mean_xrn=mean(x_rn);
var_xrn=var(x_rn);
snr_xrn=snr(y,rn);
%
disp(['mean=',num2str(mean_xrn),'variance',num2str(var_xrn),'SNR=',num2str(snr_xrn)]);
fprintf('Mean : %.4f\n', mean_xrn);
fprintf('Variance: %.4f\n', var_xrn);
fprintf('SNR: %.2f dB\n', snr_xrn);
disp(['SNR=',num2str(snr_xrn)]);
mean_xgn=mean(x_gn);
var_xgn=var(x_gn);
snr_xgn=snr(y,gn);
%disp(['mean=',num2str(mean_xgn),'variance',num2str(var_xgn),'SNR=',num2str(snr_xgn)])
;
fprintf('Mean : %.4f\n', mean_xgn);
fprintf('Variance: %.4f\n', var_xgn);
disp(['SNR=',num2str(snr_xgn)]);
mean_xwn=mean(x_wn);
var_xwn=var(x_wn);
snr_xwn=snr(y,wn);
fprintf('Mean : %.4f\n', mean_xwn);
fprintf('Variance: %.4f\n', var_xwn);
fprintf('SNR: %.2f dB\n', snr_xwn);
%disp(['mean=',num2str(mean_xwn),'variance',num2str(var_xwn),'SNR=',num2str(snr_xwn)])
;
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
(4)Generate the complex sine wave frequency 4 KHz with the N=1000, Sampling Frequency 8000
2 πfn
j +w(n)
Fs
x ( n )=√ γ e
W (n) is Added complex noise to sine wave
What happen when real part and imaginary part
γ =10 γ =100 γ =1000 Plot the real part of the signal
% Q4 complex sine wave
wnc=wgn(N,1,0)+1i*wgn(N,1,0);
g=10;
x_complex_10=sqrt(g)*exp((1i*(2*pi*(f/fs)*n)))+wnc;
figure;
subplot(3,1,1);
plot(real(x_complex_10));
title("gamma=10");
g=100;
x_complex_100=sqrt(g)*exp((1i*(2*pi*(f/fs)*n)))+wnc;
subplot(3,1,2);
plot(real(x_complex_100));
title("gamma=100");
g=1000;
x_complex_1000=sqrt(g)*exp((1i*(2*pi*(f/fs)*n)))+wnc;
subplot(3,1,3);
plot(real(x_complex_1000));
title("gamma=1000");
Mereddy Jahnavi 20011P0416 ECE-IDP V-I
Mereddy Jahnavi 20011P0416 ECE-IDP V-I