CS Lab 01
CS Lab 01
Obtained
Name Roll No
Marks (35)
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
Linear System
X[n H[n] Y[n
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')
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).
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
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);
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);
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);
% 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)]);
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
% 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']);
% 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);