0% found this document useful (0 votes)
22 views

CS Lab 01

The task involves convolving two audio files (a clap sound and hall impulse response) using Matlab. The left channels of the stereo audio files are extracted and convolved. The output is normalized between -1 to 1 and written to a new wav file. TASK-1: Convolution of audio files 1. Import input audio file clapstereo = audioread('clap.wav'); 2. Extract left channel clapmono = clapstereo(:,1); 3. Import impulse response hallstereo = audioread('hallIR.wav'); 4. Extract

Uploaded by

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

CS Lab 01

The task involves convolving two audio files (a clap sound and hall impulse response) using Matlab. The left channels of the stereo audio files are extracted and convolved. The output is normalized between -1 to 1 and written to a new wav file. TASK-1: Convolution of audio files 1. Import input audio file clapstereo = audioread('clap.wav'); 2. Extract left channel clapmono = clapstereo(:,1); 3. Import impulse response hallstereo = audioread('hallIR.wav'); 4. Extract

Uploaded by

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

Electrical Engineering Department

EE365L: Communication Systems Lab

Course Instructor: Dr. Muhammad Qasim Dated: 23rd January, 2024

Lab Instructors: Tawahaa Ahmed


Semester: 6th

Class: BSEE-21 Session: Spring-2024

Lab 1. Convolution, Correlation, Energy and Power of Signals

Obtained
Name Roll No
Marks (35)

Muhammad Mahad Azher BSEE21075

Checked on: _______________________________

Signature: _______________________________
Convolution, Correlation, Energy and Power of Signals

2.1 Objective
To learn how we can compute the convolution, correlation, energy and power of the signals
Equipment/Apparatus
1. Software: Matlab

2.2 Theory and Procedure


Following are the terms mostly used in Communication Systems
Convolution
Convolution describes the relationship between the input signal X[n], the impulse response
H[n] of an LTI system, and the output signal Y[n] (see Figure 2-1).

Linear System
X[n H[n] Y[n

𝑋[𝑛] ⨂ 𝐻[𝑛] = 𝑌[𝑛]


Figure 2-1 In
Convolution mathematical notation,
convolution is described as:
𝑌[𝑛] = 𝑋[𝑛] ⨂ 𝐻[𝑛]
The Matlab function used for Convolution is “conv”. C = conv (A, B) convolves vectors A
and B and saves the output in vector C. Example:
a = [1 0 3 1]; b
= [2 3 1 0];
d=conv(a, b)

Result:
d=

2 3 7 11 6 1 0

Correlation
Correlation is a mathematical operation that is very similar to convolution. Just as with
convolution, correlation uses two signals to produce a third signal. It shows how much similar
or different are the two signals when compared with each other. The output signal is called
the cross-correlation of the two input signals. If a signal is correlated with itself, the
resulting output signal is instead called the autocorrelation.
Autocorrelation, also known as serial correlation, is the correlation of a signal with itself
when second copy of the signal is delayed compared to the first one. In other words,
autocorrelation is the measure of similarity between the two copies of the same signal as a
function of the time lag between them.
The Matlab command used for correlation is “xcorr”. Example:
Find the autocorrelation and cross-correlation of x and y.
x = [1 0 0 0 0]; y = [0 0
1 0 0]; [xcorr_a n] =
xcorr(x, x) subplot(221)
stem(n, xcorr_a)
xlabel('tau')
ylabel('auto-corelation
of x') [xcorr_a n] =
xcorr(y, y) subplot(222)
stem(n, xcorr_a)
xlabel('tau')
ylabel('auto-corelation
of y') [xcorr_a n] =
xcorr(x, y) subplot(223)
stem(n, xcorr_a)
xlabel('tau')
ylabel('cross-corelation of x and y')

Figure 2-2 correlation

Energy and Power of the Signal


Energy and power of the signal is calculated by the following formulae

Here T = duration of the signal, and x[n] denotes discrete samples of the signal at regular
intervals (The sampled signal contains N points stretching from 0 to N-1). “NORM” function
in Matlab can be utilized for calculating the power or energy content of a signal.
Example
%Syntax: (Extracted from Matlab help) %NORM(V,
P) = sum(abs(V).^P)^(1/P).

%sample to calculate Energy and Power

T=10; %Total evaluation time


Ts=0.001; %Sampling time => 1000 samples per second Fs=1/Ts;
% Sampling period
t=[0:Ts:T]; %define simulation time

%sample function to calculate Energy and Power

x=cos(2*pi*100*t) + cos(2*pi*200*t) + sin(2*pi*300*t);


energy = (norm(x)^2); power = (norm(x)^2)/length(x);
Results: energy = 1.5004e+04
power = 1.5002

In mathematics, orthogonality is the relation of two lines at right angles to one another
(perpendicularity), and the generalization of this relation into n dimensions; and to a variety
of mathematical relations thought of as describing non-overlapping, uncorrelated, or
independent objects of some kind. In communications, multiple-access schemes are
orthogonal when an ideal receiver can completely reject arbitrarily strong unwanted signals
from the desired signal using different basis functions.
The dot product of orthogonal signals is zero. If we have two orthogonal signals x and y, then
following equation satisfies:
𝑥⃗. 𝑦⃗ = 0
Orthogonal channel
Consider you are transmitting two signals using same frequency. There will be interference
between these two signals if they are not orthogonal. Orthogonality means both signal is
having phase difference of 90 degrees. Hence, it will not interfere each other. Just like
CDMA, all the channels are orthogonal and hence we can use same frequency allocation for
all users but signals are decoded based on PN sequence which is used for spreading the
signal. Orthogonal signaling uses carriers which do not correlate with each other (inner
product or mean of mutual multiplication is zero). In case of non-dispersive channel this
signaling is very efficient, since there is no interference between carriers. However, most of
real-life channels are dispersive and inter-carrier interference is present. Moreover,
orthogonal signaling is much more sensitive to synchronization errors than non-orthogonal
one.
Shannon’s Formula
The Shannon–Hartley theorem states the channel capacity C, meaning the theoretical tightest
upper bound on the information rate of data that can be communicated at an arbitrarily low
error rate using an average received signal power S through an analog communication
channel subject to additive white Gaussian noise of power N:

Where,
C is the channel capacity in bits per second, a theoretical upper bound on the net bit rate
(information rate, sometimes denoted I) excluding error-correction codes;
B is the bandwidth of the channel in hertz (passband bandwidth in case of a bandpass signal);
S is the average received signal power over the bandwidth (in case of a carrier-modulated
passband transmission, often denoted C), measured in watts (or volts squared);
N is the average power of the noise and interference over the bandwidth, measured in watts
(or volts squared); and
S/N is the signal-to-noise ratio (SNR) or the carrier-to-noise ratio (CNR) of the
communication signal to the noise and interference at the receiver (expressed as a linear
power ratio, not as logarithmic decibels).
Example:
Plot capacity of the channel:
i) As a function of SNR (vary SNR between -10 to 30 dB, but then convert it into
linear scale)
ii) As a function of BW (vary it between 10KHz to 20 MHz)

B=10^3:10^3:10^6
SNRdb=10
SNR=db2mag(SNRdb)
C = B * log2(1+SNR);
subplot(2,1,1) plot(B,C);
xlabel('Bandwidth (Hertz)')
ylabel('Channel Capacity (Bits per Second)')
%=================
B=10^3
SNRdb=-10:30
SNR=db2mag(SNRdb) C
= B * log2(1+SNR);
subplot(2,1,2)
plot(SNR,C);
xlabel('SNR')
ylabel('Channel Capacity (Bits per Second)')
Figure 0-3 Channel Capacity
Write the codes of following exercises. Print the codes and results with proper labeling.
Attach it with the manual

2.3 Lab Tasks and Student Report

TASK-1: [4 Marks]
One of the most immediate and impressive applications of convolution can be found in the
techniques of digital audio processing. In the past 15 years, a variety of tools using
convolution have been developed and popularized.
The task is to convolution of audio files.
1. Download any two audios of your own choice from internet in .wav file format 2.
Import the input wave file x[n].
clapstereo = wavread('/<path>/clap.wav')
clapstereo = audioread('Fanfare60.wav');
[Note: you have to add the Wavefile folder to the path. Select “Selected Folders”. See figure

2-6]
Figure 0-4 Add to Path instructions
3. The audios will be shown in two columns. Each of these columns represents a
column vector in the matrix, corresponding to an audio channel(L/R). This file is
a
stereo file (it contains left and right channel information) and so we will need to
strip out the left channel. Type the following
clapmono = clapstereo(:,1)
4. Next, do the same procedure with the impulse response H(n) file
hallstereo = wavread('<path>/LongEchoHallIR.wav') hallstereo
= audioread('preamble.wav'); hallmono = hallstereo(:,1)
5. Now convolute the X[n] and H[n] out1 = conv(clapmono,
hallmono)
6. Before we can write this to a new file, we must normalize this vector so that none
of its entries is bigger than 1 in absolute value. This is because .wav files require
the amplitude of all waveforms to be between -1 and 1. (Note: Waveforms which
'clip'(exceed the maximum level) are not useful, as they play back with ugly
digital distortion. This is to be avoided, always, when working with digital
audio). To normalize the vector, we write:
% max(v) = absolute value of largest entry of v
out1normalized = out1/max(out1)
7. We now have a vector which is suitable to be exported! Let's write this to a file:
%The second parameter is the sample rate
wavwrite(out1normalized, 44100, '<path>/out1.wav')
8. Use Hands-free (ear-in or earbuds) to listen to the audio
9. Now do the same for “clap” and “drumloop” sound or some other combinations
of your liking.

cleopetra = audioread('la-foule.wav');
stripleft = cleopetra(:,1);

cleopetra1 = audioread('harry-james-his-orchestra-its-been-a-long-long-time.wav');
stripleft1 = cleopetra1(:,1);

output1 = conv(stripleft,stripleft1);

output1normalized = output1/max(output1);

audiowrite('lab_01_task_01_out1.wav', output1normalized, 44100);

This task is evaluated during lab in run-time, and instructor comments are given below:

TASK-2: [2 Marks]
Find auto-correlation and cross-correlation of PN_seq and show results in the form of graph
(Like the Example given in correlation i.e. section 2.3.2).
PN_seq_1 = randn(10,1)
PN_seq_2 = randn(10,1)
Please paste the output graph on next page:
% Compute auto-correlation of cleopetra (stripleft)
[auto_corr_cleopetra, lag_cleopetra] = xcorr(stripleft);

% Plot auto-correlation of cleopetra (stripleft)


figure;
stem(lag_cleopetra, auto_corr_cleopetra);
title('Auto-correlation of cleopetra');
xlabel('Lag');
ylabel('Correlation');

% Compute auto-correlation of cleopetra1 (stripleft1)


[auto_corr_cleopetra1, lag_cleopetra1] = xcorr(stripleft1);

% Plot auto-correlation of cleopetra1 (stripleft1)


figure;
stem(lag_cleopetra1, auto_corr_cleopetra1);
title('Auto-correlation of cleopetra1');
xlabel('Lag');
ylabel('Correlation');

% Compute cross-correlation between cleopetra (stripleft) and cleopetra1 (stripleft1)


[cross_corr_cleopetra_cleopetra1, lag_cleopetra_cleopetra1] = xcorr(stripleft, stripleft1);

% Plot cross-correlation between cleopetra (stripleft) and cleopetra1 (stripleft1)


figure;
stem(lag_cleopetra_cleopetra1, cross_corr_cleopetra_cleopetra1);
title('Cross-correlation between cleopetra and cleopetra1');
xlabel('Lag');
ylabel('Correlation');
TASK-3: [1 Marks]
Execute the following code and explain the difference between convolution and correlation.
x = [1 2 3 4]; y
= [-1 2 1 -1];
conv(x,y)
conv(fliplr(x),y)
xcorr(x,y)
 Convolution combines two signals to produce a third, while autocorrelation
measures the similarity of a signal with a delayed version of itself.
 Convolution is a more general operation used in various contexts, including signal
processing and linear systems analysis.
 Autocorrelation is a specific operation focused on self-similarity and is often used
in the analysis of time-series data.

TASK-4: [1 Marks]
Find whether or not Sin(x) and Cos(x) are orthogonal or not for period of 0 ≥ t ≥ 2π. And also
find orthogonality of Sin(x) with Sin(2x) and Sin(3x) for period of 0 ≥ t ≥ 2π. Show results in
the form of graphs.
[Hint: Specify the argument x in radians, not in degrees. For example, use π to specify an
angle of 180o. Use dot (a, b) or sum(x.*y)]
Code:
% Define the symbolic variable x
syms x;
% Define the functions
f1 = sin(x);
f2 = cos(x);
f3 = sin(2*x);
f4 = sin(3*x);

% Specify the interval in degrees


a_deg = 0;
b_deg = 360;

% Convert the interval to radians


a = deg2rad(a_deg);
b = deg2rad(b_deg);

% Compute inner products


inner_product_1 = int(f1*f2, x, a, b);
inner_product_2 = int(f1*f3, x, a, b);
inner_product_3 = int(f1*f4, x, a, b);

% Display results
disp(['Inner Product of sin(x) and cos(x): ', char(inner_product_1)]);
disp(['Inner Product of sin(x) and sin(2x): ', char(inner_product_2)]);
disp(['Inner Product of sin(x) and sin(3x): ', char(inner_product_3)]);

% Plot the functions in separate figures


figure;
fplot(f1, [a, b], 'DisplayName', 'sin(x)');
title('Function: sin(x)');
xlabel('x (degrees)');
ylabel('Function Values');

figure;
fplot(f2, [a, b], 'DisplayName', 'cos(x)');
title('Function: cos(x)');
xlabel('x (degrees)');
ylabel('Function Values');

figure;
fplot(f3, [a, b], 'DisplayName', 'sin(2x)');
title('Function: sin(2x)');
xlabel('x (degrees)');
ylabel('Function Values');

figure;
fplot(f4, [a, b], 'DisplayName', 'sin(3x)');
title('Function: sin(3x)');
xlabel('x (degrees)');
ylabel('Function Values');
Hence they are all orthogonal

TASK-5: [1 Marks]
Compute the effect of doubling the bandwidth BW and doubling the SNR on channel capacity
(using Shannon’s formula) and see which parameter (among BW and SNR) has dominating
effect. SNR: -10 dB, Bandwidth: 20KHz

Code:
% Given values
SNR_dB = -10; % SNR in decibels
BW = 20e3; % Bandwidth in hertz

% Convert SNR from dB to linear scale


SNR_linear = 10^(SNR_dB/10);
% Initial channel capacity
C_initial = BW * log2(1 + SNR_linear);

% Scenario 1: Doubling the bandwidth


BW_double = 2 * BW;
C_double_BW = BW_double * log2(1 + SNR_linear);

% Scenario 2: Doubling the SNR


SNR_double = 2 * SNR_linear;
C_double_SNR = BW * log2(1 + SNR_double);

% Display results
disp('Scenario 1: Doubling the Bandwidth');
disp(['Initial Channel Capacity: ', num2str(C_initial), ' bits per second']);
disp(['Channel Capacity with Doubling the Bandwidth: ', num2str(C_double_BW), ' bits
per second']);

disp('Scenario 2: Doubling the SNR');


disp(['Initial Channel Capacity: ', num2str(C_initial), ' bits per second']);
disp(['Channel Capacity with Doubling the SNR: ', num2str(C_double_SNR), ' bits per
second']);

% Plotting
subplot(2, 1, 1);
plot([BW, BW_double], [C_initial, C_double_BW], '-o');
xlabel('Bandwidth (Hertz)');
ylabel('Channel Capacity (Bits per Second)');
title('Effect of Doubling Bandwidth');

subplot(2, 1, 2);
SNRdb_range = -10:30;
SNR_range = db2mag(SNRdb_range);
C_range = BW * log2(1 + SNR_range);
plot(SNRdb_range, C_range, '-o');
xlabel('SNR (dB)');
ylabel('Channel Capacity (Bits per Second)');
title('Effect of Changing SNR');
TASK-6: [1 Marks]
Calculate RMS value of signal: Sin(2x) from 0 to 2π.

Code:
% Define the function sin(2x)
f = @(x) sin(2*x);

% Define the interval [a, b]


a = 0;
b = 2*pi;

% Calculate the RMS value using the integral


rms_value = sqrt(1/(b - a) * integral(@(x) f(x).^2, a, b));

% Display the RMS value


disp(['RMS value of sin(2x) from 0 to 2π: ', num2str(rms_value)]);
Result:

You might also like