0% found this document useful (0 votes)
24 views19 pages

ASSP Lab-1

The document provides MATLAB code for generating random sequences from various probability distributions including uniform, Gaussian, Rayleigh, and Rician distributions. It also explains the AR, MA, and ARMA models for time series analysis, along with their power spectrum calculations. Additionally, it details an LMS-based noise cancellation system, including its model, algorithm steps, and implementation in MATLAB.

Uploaded by

pskowsalya12
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)
24 views19 pages

ASSP Lab-1

The document provides MATLAB code for generating random sequences from various probability distributions including uniform, Gaussian, Rayleigh, and Rician distributions. It also explains the AR, MA, and ARMA models for time series analysis, along with their power spectrum calculations. Additionally, it details an LMS-based noise cancellation system, including its model, algorithm steps, and implementation in MATLAB.

Uploaded by

pskowsalya12
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/ 19

EXP NO 2: Generation of random sequences satisfying the given probability distributions such as

uniform, gaussian, Rayleigh and rician

1. Uniform Distribution

In MATLAB, you can use the rand function to generate random values uniformly distributed between
0 and 1. For a specific range [a,b] you can scale and shift the output.

% Parameters for Uniform Distribution

a = 0; % Lower bound

b = 1; % Upper bound

num_samples = 1000; % Number of samples

% Generate random samples

uniform_samples = a + (b - a) * rand(1, num_samples);

% Plot the results

figure;

histogram(uniform_samples, 30, 'Normalization', 'pdf');

title('Uniform Distribution');

xlabel('Value');

ylabel('Probability Density');
2. Gaussian (Normal) Distribution

In MATLAB, the randn function generates random samples from a standard normal distribution
(mean = 0, standard deviation = 1). You can scale and shift the distribution to any mean and standard
deviation.

% Parameters for Gaussian Distribution

mu = 0; % Mean

sigma = 1; % Standard deviation

num_samples = 1000; % Number of samples

% Generate random samples

gaussian_samples = mu + sigma * randn(1, num_samples);

% Plot the results

figure;

histogram(gaussian_samples, 30, 'Normalization', 'pdf');

title('Gaussian Distribution');

xlabel('Value');

ylabel('Probability Density');
3. Rayleigh Distribution

To generate random values from a Rayleigh distribution, you can use the raylrnd function (introduced
in MATLAB R2013a). The Rayleigh distribution requires one parameter, which is the scale parameter
σ\sigmaσ.

% Parameters for Rayleigh Distribution

sigma = 1; % Scale parameter

num_samples = 1000; % Number of samples

% Generate random samples

rayleigh_samples = raylrnd(sigma, [1, num_samples]);

% Plot the results

figure;

histogram(rayleigh_samples, 30, 'Normalization', 'pdf');

title('Rayleigh Distribution');

xlabel('Value');

ylabel('Probability Density');
4. Rician Distribution

MATLAB does not have a built-in function for the Rician distribution, but you can generate Rician
distributed random variables by using the relationship with the Rayleigh distribution. A Rician
random variable can be generated as:

X=(A+Z1)2+Z22X = \sqrt{(A + Z_1)^2 + Z_2^2}X=(A+Z1)2+Z22

Where AAA is the line-of-sight component, and Z1,Z2Z_1, Z_2Z1,Z2 are independent Gaussian
random variables with mean zero and standard deviation σ\sigmaσ.

% Parameters for Rician Distribution

A = 2; % Line-of-sight component

sigma = 1; % Noise standard deviation

num_samples = 1000; % Number of samples

% Generate random samples from Gaussian distributions

Z1 = sigma * randn(1, num_samples);

Z2 = sigma * randn(1, num_samples);

% Generate Rician samples

rician_samples = sqrt((A + Z1).^2 + Z2.^2);

% Plot the results

figure;

histogram(rician_samples, 30, 'Normalization', 'pdf');

title('Rician Distribution');

xlabel('Value');

ylabel('Probability Density');
EXP NO:7
AR (Autoregressive) Model

The AR model represents the current value of a time series as a linear combination of its previous
values (lags) plus noise.

MA (Moving Average) Model

The MA model represents the current value of a time series as a linear combination of current and
past white noise inputs.

ARMA (Autoregressive Moving Average) Model

The ARMA model combines the AR and MA processes, representing the current value as a
combination of past values and past white noise.

% Power Spectrum of AR, MA, and ARMA Models

clc; clear; close all;

% Parameters

N = 1024; % Number of samples

sigma = 1; % Standard deviation of noise

fs = 1; % Sampling frequency

%% 1. AR Model: Power Spectrum

a_AR = [1 -0.75 0.5]; % AR coefficients

noise = sigma * randn(N, 1); % White noise input

y_AR = filter(1, a_AR, noise); % Generate AR process

% Power Spectral Density (PSD) using periodogram

[PSD_AR, f_AR] = periodogram(y_AR, [], [], fs);

% Plot AR Power Spectrum

figure;

subplot(3,1,1);

plot(f_AR, 10*log10(PSD_AR));

title('Power Spectrum of AR Model');

xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');

grid on;

%% 2. MA Model: Power Spectrum

b_MA = [1 0.5 -0.25]; % MA coefficients

noise = sigma * randn(N, 1); % White noise input

y_MA = filter(b_MA, 1, noise); % Generate MA process

% Power Spectral Density (PSD) using periodogram

[PSD_MA, f_MA] = periodogram(y_MA, [], [], fs);

% Plot MA Power Spectrum

subplot(3,1,2);

plot(f_MA, 10*log10(PSD_MA));

title('Power Spectrum of MA Model');

xlabel('Frequency (Hz)');

ylabel('Power/Frequency (dB/Hz)');

grid on;

%% 3. ARMA Model: Power Spectrum

a_ARMA = [1 -0.75 0.5]; % AR coefficients

b_ARMA = [1 0.5]; % MA coefficients

noise = sigma * randn(N, 1); % White noise input

y_ARMA = filter(b_ARMA, a_ARMA, noise); % Generate ARMA process

% Power Spectral Density (PSD) using periodogram

[PSD_ARMA, f_ARMA] = periodogram(y_ARMA, [], [], fs);

% Plot ARMA Power Spectrum

subplot(3,1,3);

plot(f_ARMA, 10*log10(PSD_ARMA));
title('Power Spectrum of ARMA Model');

xlabel('Frequency (Hz)');

ylabel('Power/Frequency (dB/Hz)');

grid on;

%% Display All Spectra

sgtitle('Power Spectrum of AR, MA, and ARMA Models');

Code Explanation

1. AR Model:

o AR coefficients a=[1,−0.75,0.5] are used.

o A white noise input is filtered using filter() to generate the AR process.

o Periodogram computes the Power Spectral Density (PSD).

2. MA Model:

o MA coefficients b=[1,0.5,−0.25] are used.

o White noise input is filtered with the MA model.

3. ARMA Model:

o Combines AR and MA components using coefficients a and b

o White noise input is passed through both AR and MA filters.

4. Power Spectrum:

o The periodogram function is used to estimate the PSD for each process:
[Pxx,f]=periodogram(y,[],[],fs)

o The PSD is plotted in decibels: 10⋅log10(PSD)


EXP No:10
1. Overview of the LMS-based Noise Cancellation System

The LMS-based adaptive filter works by:

 Estimating and subtracting noise (interference) from the noisy signal.


 Adapting the filter coefficients iteratively to minimize the error (difference between the
desired signal and the estimated output).

2. System Model

The LMS-based noise cancellation system typically includes:

 Primary Input: d(n)d(n)d(n) → Contains the desired signal corrupted by noise s(n)+n1(n)s(n)
+ n_1(n)s(n)+n1(n).

 Reference Input: x(n)x(n)x(n) → Contains a noise signal n2(n)n_2(n)n2(n) that is correlated


with n1(n)n_1(n)n1(n).

 Adaptive Filter: Estimates the noise n^1(n)\hat{n}_1(n)n^1(n) and subtracts it from the
primary input to extract the desired signal s(n)s(n)s(n).

The output signal is:

e(n)=d(n)−y(n),e(n) = d(n) - y(n),e(n)=d(n)−y(n),


where y(n)y(n)y(n) is the output of the adaptive filter, and e(n)e(n)e(n) is the error signal (desired
output).

The LMS algorithm updates the filter coefficients w(n)w(n)w(n) iteratively to minimize the error.

3. LMS Algorithm Steps

1. Initialization:

o Set the adaptive filter order MMM (number of coefficients).

o Initialize the filter weights ( w(n) = [w_0(n), w_1(n), ..., w_{M-1}(n)]

Below is the MATLAB code to implement an adaptive filter using the LMS algorithm for noise
cancellation.

% LMS Adaptive Filter: Modular Implementation

clc; clear; close all;

% Parameters

N = 1000; % Number of samples

M = 16; % Filter order


mu = 0.01; % Step size

% Generate Signals

n = 0:N-1;

signal = sin(2*pi*0.01*n); % Desired signal

noise_ref = randn(1, N); % Reference noise

noise = filter([1 -0.9], 1, noise_ref); % Colored noise

desired = signal + noise; % Primary input: signal + noise

% Call LMS Function

[output, error] = lms_filter(noise_ref, desired, M, mu);

% Plot Results

figure;

subplot(3,1,1); plot(n, desired); title('Primary Input (Signal + Noise)');

subplot(3,1,2); plot(n, output); title('LMS Filter Output');

subplot(3,1,3); plot(n, error); title('Error Signal (Cleaned Signal)');

xlabel('Sample Index');

% LMS Function

function [output, error] = lms_filter(noise_ref, desired, M, mu)

% Initialize Variables

N = length(noise_ref);

w = zeros(M, 1); % Filter weights

x = zeros(M, 1); % Input buffer

output = zeros(1, N); % Filter output

error = zeros(1, N); % Error signal

% LMS Algorithm

for i = 1:N

% Update input buffer


x = [noise_ref(i); x(1:M-1)];

% Filter output

y = w' * x;

output(i) = y;

% Error calculation

e = desired(i) - y;

error(i) = e;

% Update filter weights

w = w + 2 * mu * e * x;

end

end

You might also like