SNS Lab 4
SNS Lab 4
Lab 04
sq08401
06/02/2025
Code:
%Task 1a
x = [2 2 4 5];
y = [4 5 4 0];
[r,k] = xcorr(x,y)
Results:
r = 1×7
k = 1×7
-3 -2 -1 0 1 2 3
%Task 1b
Code:
%Task 1c
subplot(1,3,1);
stem(x);
title('(x)');xlabel('Samples');ylabel('Amplitude');
subplot(1,3,2);
stem(y);
title('(y)');xlabel('Samples');ylabel('Amplitude');
subplot(1,3,3);
stem(k,r);
title('Correlation Output');xlabel('lag(k)');ylabel('Amplitude');
Results:
%Task 1d
Code:
%Task 2
clc
clear all
close all
x = [3,4,5,8]
Results:
x = 1×4
3 4 5 8
[r,k] = xcorr(x)
Results:
r = 1×7
k = 1×7
-3 -2 -1 0 1 2 3
stem(k,r)
title('AutoCorrelation Output');xlabel('Lag(k');ylabel('Amplitude')
Results:
% Auto-correlation captures the signal's energy at zero lag, decreasing
%Task 3
Code:
%search
[x , Fs_x] = audioread('speech.wav');
sound(y, Fs_y)
pause(2) %to insert a pause of 2 seconds between the two audio files
sound(x, Fs_x)
cmax = max(abs(correlation));
subplot 311
plot(x);title("Speech Signal");ylabel("Amplitude")
subplot 312
plot(y);title("Speech Segment");ylabel("Amplitude")
subplot 313
plot(lag,Nonlinear_Effect);title("Correlation");xlabel("Lag")
Results:
[~,index] = max(abs(correlation))
location=lag(index);
sound(x(location:end),Fs_x)
%Task 4
[x , Fs_x] = audioread('speech.wav')
x_noise = randn(size(x))
noise1 = x_noise/10
noise2 = x_noise/90
x_new1 = x + (noise2);
x_new2 = x + (noise1)
sound(x_new1,Fs_x);subplot 311
plot(x);title('Original Signal');ylabel('Amplitude')
subplot 312
subplot 313
[correlation,lag] = xcorr(x,y)
correlation = 421173×1
lag = 1×421173
plot(correlation);title("Correlation Signal");xlabel("Lag")
Results:
snr = mean(x.^2)/(mean((x_noise/90).^2))
snr = 6.8822
%Task 4 (extra noisy)
Code:
sound(x_new2,Fs_x);
subplot 311
plot(x);title('Original Signal');ylabel('Amplitude')
subplot 312
subplot 313
[correlation,lag] = xcorr(x,y)
correlation = 421173×1
lag = 1×421173
plot(correlation);title("Correlation Signal");xlabel("Lag")
Results:
snr = (mean(x.^2))/(mean((x_noise/10).^2))
snr = 0.0850
%Post Lab
Code:
clc
close all;
clear all;
[audioSeg1, fsSeg1] = audioread('okay_1.wav');
sound(audioSeg1, fsSeg1);
pause(2);
sound(audioSeg2, fsSeg2);
pause(2);
sound(audioSeg3, fsSeg3);
pause(2);
sound(audioSeg4, fsSeg4);
pause(2);
sound(mainAudio, fsMain);
maxVal1 = max(abs(cc1));
maxVal2 = max(abs(cc2));
maxVal3 = max(abs(cc3));
nonlinearCorr3 = (1.1 * cc3 / maxVal3).^3;
maxVal4 = max(abs(cc4));
subplot(4,1,1);
plot(lag1, nonlinearCorr1);
subplot(4,1,2);
plot(lag2, nonlinearCorr2);
subplot(4,1,3);
plot(lag3, nonlinearCorr3);
subplot(4,1,4);
plot(lag4, nonlinearCorr4);
%% Locating Peak Correlations and Play Main Signal from Those Points
Code:
peak1= max(nonlinearCorr1)
peak1 = 1.2049
index1 = (nonlinearCorr1==peak1);
location1=lag1(index1);
pause(2)
sound(mainAudio(location1:end),fsMain);
peak2= max(nonlinearCorr2)
peak2 = 1.2661
index2=(nonlinearCorr2==peak2);
location2=lag2(index2);
pause(2)
sound(mainAudio(location2:end),fsMain);
peak3= max(nonlinearCorr3)
peak3 = 1.3310
index3=(nonlinearCorr3==peak3);
location3 =lag3(index3);
pause(2)
sound(mainAudio(location3:end),fsMain);
peak4= max(nonlinearCorr4)
peak4 = 1.3310
index4=(nonlinearCorr4==peak4);
location4=lag4(index4);
pause(2)
sound(mainAudio(location4:end),fsMain);
threshold = 2.3188