0% found this document useful (0 votes)
2 views13 pages

SNS Lab 4

The document outlines a lab submission involving signal processing tasks, including cross-correlation and auto-correlation of audio signals. It details the implementation of MATLAB code for analyzing signal similarity, generating plots, and applying nonlinear transformations to audio segments. Additionally, it discusses the effects of noise on signals and the challenges in detecting audio patterns under noisy conditions.

Uploaded by

hamzaahmed200072
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)
2 views13 pages

SNS Lab 4

The document outlines a lab submission involving signal processing tasks, including cross-correlation and auto-correlation of audio signals. It details the implementation of MATLAB code for analyzing signal similarity, generating plots, and applying nonlinear transformations to audio segments. Additionally, it discusses the effects of noise on signals and the challenges in detecting audio patterns under noisy conditions.

Uploaded by

hamzaahmed200072
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/ 13

SNS Lab Submission

Lab 04

Syed Hamza Ahmed Quadri

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

-0.0000 8.0000 18.0000 34.0000 48.0000 41.0000 20.0000

k = 1×7

-3 -2 -1 0 1 2 3

%The range of the lag is -3 to 3

%Task 1b

%For the case n1>n2: the range would be no longer than n2

%For the case n2>n1: The range would be no longer than n1

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

%The `xcorr` function measures signal similarity at different lags.

%If one signal is a shifted version of the other, the peak


correlation

%appears at the lag corresponding to that shift, not at zero lag.

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

24.0000 47.0000 72.0000 114.0000 72.0000 47.0000 24.0000

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

%as lag deviates.

%Task 3

Code:

[y , Fs_y] = audioread('speech_segment.wav'); % segment of signal we


want to

%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)

[correlation , lag] = xcorr(x,y);

cmax = max(abs(correlation));

Nonlinear_Effect = (1.1* correlation/cmax).^3 ;

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

plot(x_new1);title("Noisy Signal using noise/90");ylabel("Amplitude")

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

plot(x_new2);title("Noisy Signal using noise/10");ylabel("Amplitude")

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

%The Snr value is 0.0850 when the sound eventually becomes


unrecognisable

%Post Lab

Code:

clc

close all;

clear all;
[audioSeg1, fsSeg1] = audioread('okay_1.wav');

[audioSeg2, fsSeg2] = audioread('okay_2.wav');

[audioSeg3, fsSeg3] = audioread('okay_3.wav');

[audioSeg4, fsSeg4] = audioread('okay_4.wav');

[mainAudio, fsMain] = audioread('main_audio.wav'); % Complete signal

sound(audioSeg1, fsSeg1);

pause(2);

sound(audioSeg2, fsSeg2);

pause(2);

sound(audioSeg3, fsSeg3);

pause(2);

sound(audioSeg4, fsSeg4);

pause(2);

sound(mainAudio, fsMain);

%% Compute Cross-Correlation and Apply Nonlinear Transformation

[cc1, lag1] = xcorr(mainAudio, audioSeg1);

maxVal1 = max(abs(cc1));

nonlinearCorr1 = (1.1 * cc1 / maxVal1).^3;

[cc2, lag2] = xcorr(mainAudio, audioSeg2);

maxVal2 = max(abs(cc2));

nonlinearCorr2 = (1.1 * cc2 / maxVal2).^3;

[cc3, lag3] = xcorr(mainAudio, audioSeg3);

maxVal3 = max(abs(cc3));

nonlinearCorr3 = (1.1 * cc3 / maxVal3).^3;

[cc4, lag4] = xcorr(mainAudio, audioSeg4);

maxVal4 = max(abs(cc4));

nonlinearCorr4 = (1.1 * cc4 / maxVal4).^3;

%% Plotting the Nonlinear Correlations

subplot(4,1,1);

plot(lag1, nonlinearCorr1);

title('Correlation for Segment 1');xlabel("Lag")

subplot(4,1,2);

plot(lag2, nonlinearCorr2);

title('Correlation for Segment 2');xlabel("Lag")

subplot(4,1,3);

plot(lag3, nonlinearCorr3);

title('Correlation for Segment 3');xlabel("Lag")

subplot(4,1,4);

plot(lag4, nonlinearCorr4);

title('Correlation for Segment 4');xlabel("Lag")


Results:

%% 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=min([maxVal1 maxVal2 maxVal3 maxVal4 ])

threshold = 2.3188

%While this function may detect a password under ideal conditions,it's

%unreliable against noise and variations in speech.More complex methods

%may be needed for a more reliable password detector.

You might also like