BS Pra

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Participatory Report Assessment

Branch : ECE-B
Subject : Basic Simulation Laboratory (BS Lab)
Faculty : Ms.T.Sravanthi/Mrs.Y.Roji

Title:

Scilab code to generate a random


signal, add noise, and apply a
moving average filter

Presented By :
K. Sai Prasanna - 22P61A0490,
M. Rakshitha - 22P61A0496,
M. Dhana Laksmi - 22P61A04A0.
Scilab code to generate a random signal, add noise
and apply a moving average filter

Aim: To generate a random signal, adding noise and applying a moving


average filter.
Software: Scilab 6.0.1 and system loaded with Linux flavor of Ubuntu 16.04
LTS.
Pre-requisites: Ensure that Scilab is installed on your system. You can
download and install Scilab from the official Scilab website.
Theory: This program demonstrates the process of generating a random
signal, adding noise to it and then applying a simple moving average filter to
reduce the noise. The resulting signals (original, noisy, and filtered) are
plotted for visual comparison.
Inbuit functions used in this program:

clc, clear, close These functions are used for managing the console,
workspace and graphics windows.

sin The sin function is used to generate a sinusoidal


signal. It calculates the sine of an angle.

rand The rand function generates random numbers. In this


code, it is used to create Gaussian noise.

filter The filter function is applied to implement the moving


average filter.

subplot, plot, xtitle These functions are part of the plotting capabilities in
Scilab. They are used to organize subplots and
visualize signals. xtitle is used to set the lables and
titles for the graphs.
Program explanation:
Parameter Definition:
‘duration = 2;’ - Specifies the duration of the signals in seconds.
‘sampling_rate = 1000;’ - Sets the number of samples per second.
‘noise_amplitude = 0.2;’ -Determines the amplitude of the added noise.
Time Values Generation:
‘time = 0:1/sampling_rate:duration;’
Creates a time vector from 0 to the specified duration with a step size of
1/sampling_rate.

Original Signal Generation:


‘original_signal = sin(2*%pi*5*time) + 0.5*sin(2*%pi*10*time);’
Generates a signal composed of two sinusoidal components.
Noise Addition:
‘noisy_signal = original_signal + noise_amplitude * rand(1, length(time),
'normal');’
Introduces Gaussian noise to the original signal.
Moving Average Filter Design:
‘window_size = 50;’ - Sets the size of the moving average filter window.
‘filter_coefficients = ones(1, window_size) / window_size;’ - Defines the
filter coefficients for a simple moving average.
Filter Application:
‘filtered_signal = filter(filter_coefficients, 1, noisy_signal);’
Applies the moving average filter to the noisy signal.
Plotting:
Organizes the plots into a 3x1 grid using ‘subplot’.
Visualizes the original, noisy, and filtered signals using ‘plot’.
Adds labels and titles for clarity.
Program:
// Generating a random signal, adding noise, and applying a moving average
filter
// Managing console and graphics windows
clc;
clear;
close;
// Define the parameters
duration = 2; // in seconds
sampling_rate = 1000; // in samples per second
noise_amplitude = 0.2;
// Generate time values
time = 0:1/sampling_rate:duration;
// Generate a random signal
original_signal = sin(2*%pi*5*time) + 0.5*sin(2*%pi*10*time);
// Add noise to the signal
noisy_signal = original_signal + noise_amplitude * rand(1, length(time),
'normal');
// Design a simple moving average filter
window_size = 50;
filter_coefficients = ones(1, window_size) / window_size;
// Apply the filter to the noisy signal
filtered_signal = filter(filter_coefficients, 1, noisy_signal);
// Plot the signals
subplot(3,1,1)
plot(time, original_signal)
xtitle("Original signal", "Time (seconds)", "Amplitude")

subplot(3,1,2)
plot(time, noisy_signal)
xtitle("Noisy signal", "Time (seconds)", "Amplitude")

subplot(3,1,3)
plot(time, filtered_signal)
xtitle("Filtered signal (moving average)", "Time (seconds)", "Amplitude")
OUTPUT

1. Original Signal:
• Clean sinusoidal waveform with two components.
• Represents an ideal, noise-free signal.
2. Noisy Signal:
• Signal after introducing Gaussian noise.
• Demonstrates the impact of random disturbances on the original
signal.
3. Filtered Signal (Moving Average):
• Result after applying a moving average filter.
• Smoother waveform compared to the noisy signal, showcasing
noise reduction.
These three output waves, displayed in subplots, provide a succinct
representation of the signal processing stages—initial signal generation,
introduction of noise, and the successful application of a filter for noise
reduction.
Execution Steps:
1. Open Scilab and create a new script.
2. Copy and paste the code into the script.
3. Save the script with an appropriate name and extension (e.g., .sce).
4. Run the script, and inspect the console for any output.
5. Explore the graphical plots displaying the original, noisy, and filtered
signals.
Notes:
- Ensure that Scilab is properly installed and configured on your system.
- Check for any error messages in the console and troubleshoot if needed.
- Adjust parameters as desired to explore different scenarios.
- Save your work periodically to preserve changes in the script.
By following these detailed execution steps, you should be able to run the
Scilab program, visualize the signals.

Applications:
1. Signal Processing Education:
This program can be used as an educational tool to teach fundamental
concepts in signal processing. Students can observe the impact of noise
on signals and the effectiveness of a moving average filter in noise
reduction.
2. Noise Reduction in Sensor Data:
This program simulates a situation where a signal (representing sensor
data) is corrupted by noise, and the moving average filter can be
analogous to a noise reduction technique applied in sensor systems.
3. Audio Signal Processing:
Similar concepts can be applied in audio signal processing. The original
signal might represent a clean audio signal, and the noisy signal could
simulate the introduction of background noise. The moving average filter
can be compared to simple noise reduction techniques applied in audio
processing.
4. Vibration Analysis:
In vibration analysis, sensors may capture signals from machinery, and
these signals might be affected by various sources of noise. Applying
filters to such signals can help in extracting relevant information about
the machinery's condition.
5. Communication Systems:
This program can be a simplified representation of the challenges faced in
communication channels and how filtering techniques contribute to
improving signal quality.
6. Biomedical Signal Processing:
Biomedical signals, such as electrocardiograms (ECG) or
electromyograms (EMG), may be affected by various types of noise.
Filtering techniques are essential in enhancing the quality of biomedical
signals for accurate analysis.
7. Control Systems:
In control systems, sensor signals may be subject to noise. Applying
filters helps in obtaining more accurate measurements, which is critical
for the stability and performance of control systems.
8. Machine Learning Preprocessing:
Noisy signals are common in datasets used for machine learning. This
program provides a basic illustration of preprocessing steps, where noisy
signals are filtered to improve the quality of input features for machine
learning models.
These applications showcase the versatility of the program and how similar
concepts are applied in various fields to address challenges related to signal
quality and noise reduction.
Observations:
- The noisy signal exhibits fluctuations and irregularities due to the added
noise.
- The filtered signal appears smoother compared to the noisy signal,
indicating the noise reduction achieved by the moving average filter.

Conclusion:
1. Signal Generation:
- The program generates an original signal composed of two sinusoidal
components. This represents a theoretical, clean signal without any noise.

2. Noise Addition:
- Gaussian noise is added to the original signal, simulating real-world
scenarios where signals are often corrupted by random disturbances.

3. Moving Average Filtering:


- A simple moving average filter is designed and applied to the noisy signal.
The filter aims to smooth out the high-frequency noise components while
preserving the underlying signal.

4. Plotting:
- The program visualizes the original, noisy, and filtered signals in a 3x1
grid of subplots. Each subplot provides insight into the impact of noise and
the effectiveness of the moving average filter.

Result:
This Scilab program demonstrates the application of a moving average filter
in signal processing. The initial generation of a clean, sinusoidal signal is
followed by the addition of Gaussian noise to simulate real-world signal
corruption. The program then applies a moving average filter to reduce noise
while preserving the essential characteristics of the original signal.
The visual representation of the signals in subplots provides a concise and
informative overview of the impact of noise and the effectiveness of the
filtering process.
Overall, the program serves as a practical example for learners to understand
and experiment with basic noise reduction techniques in signal processing.

You might also like