SIP Assignments
SIP Assignments
Study OF MATLAB
Applications of MATLAB
1. Engineering and Scientific Research:
a. Simulation and modeling of systems (e.g., electrical circuits, mechanical
systems).
b. Data analysis and visualization for experimental results.
1. Signal and Image Processing:
Processing and analysis of digital signals and images.
Development of algorithms for noise reduction, feature extraction, etc.
1. Machine Learning and AI:
Training and deploying machine learning models using built-in tools.
0901EC231118
Day 1
2. Financial Modeli
Portfolio optimization, risk analysis, and time-series forecasting.
1. Education:
Aiding students in understanding complex mathematical and engineering concepts.
Advantages of MATLAB
Ease of Use: Intuitive syntax suitable for beginners and experts alike.
Extensive Documentation: Comprehensive help files and user community support.
Versatility: Suited for various applications across disciplines.
Performance: Optimized for numerical computation and efficient handling of large
datasets.
Menu bar
1 4,6 and 7 5
2
3
0901EC231118
Day 1
1- CURRENT DIRECTORY
2-WORKSPACE
3-COMMAND HISTORY
4-COMMAND WINDOW
5-RESULT WINDOW
6-EDITOR WINDOW (save ,run)
7-FIGURE WINDOW (plots ,images)
“Ver” command is used to find the version of MATLAB
R2024b Means Version is released in 2024 july- december
0901EC231118
Day 2
2) MATRIX MANIPULATION:
Matrix Creation-
% Row vector
row_vector = [1, 2, 3];
% Column vector
column_vector = [1; 2; 3];
% 2x3 matrix
matrix_2x3 = [1, 2, 3; 4, 5, 6];
% 3x3 identity matrix
identity_matrix = eye(3);
% Random 3x3 matrix with values between 0 and 1
random_matrix = rand(3);
% Zero matrix of size 3x3
zero_matrix = zeros(3);
OUTPUT-
0901EC231118
Day 2
Matrix Addition And Substraction-
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
% Matrix addition
C = A + B;
% Matrix subtraction
D = A - B;
OUTPUT-
0901EC231118
Day 2
Matrix Multiplication-
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
% Matrix multiplication (dot product)
E = A * B;
% Element-wise multiplication
F = A .* B;
v = [1; 2];
OUTPUT-
0901EC231118
Day 2
Matrix Transpose-
A = [1, 2; 3, 4];
B = A'; % Transpose of A
OUTPUT-
Matrix Inverse-
A = [1, 2; 3, 4];
inv_A = inv(A); % Inverse of A
OUTPUT-
0901EC231118
Day 2
3) PLOTTING SINGLE WAVE-
% time vector
t = 0:0.01:2*pi; % Time from 0 to 2*pi
% sine wave
y = sin(t); % Sine of time vector t
% Plot the sine wave
plot(t, y); % Plot with blue line and width 2
xlabel('Time (seconds)'); % Label for the x-axis
ylabel('Amplitude'); % Label for the y-axis
title('Single Sine Wave'); % Title of the plot
OUTPUT-
0901EC231118
Day2
4) SINE, COSINE AND TAN WAVE-
0901EC231118
Day 2
% Plot the tangent wave
subplot(3,1,3);
plot(t, y_tan);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Tangent Wave (f = 3 Hz)');
ylim([-10 10]); % Limit the y-axis for better visibility of the tangent wave
OUTPUT-
0901EC231118
Day2
5) SUBPLOTS-
% Create a 3x2 grid of subplots and plot sine waves with different frequencies
0901EC231118
Day 2
% Plot cosine wave with frequency f1 in the third subplot
subplot(3,2,3); % Position 3 in a 3x2 grid
y_cos1 = cos(2*pi*f1*t); % Cosine wave with frequency f1
plot(t, y_cos1, 'g-');
xlabel('Time (seconds)');
ylabel('Amplitude');
title(['Cosine Wave (f = ', num2str(f1), ' Hz)']);
0901EC231118
Day2
% Plot tangent wave with frequency f2 in the sixth subplot
subplot(3,2,6); % Position 6 in a 3x2 grid
y_tan2 = tan(2*pi*f2*t); % Tangent wave with frequency f2
plot(t, y_tan2, 'y-');
xlabel('Time (seconds)');
ylabel('Amplitude');
title(['Tangent Wave (f = ', num2str(f2), ' Hz)']);
ylim([-10 10]); % Limit y-axis for tangent wave
OUTPUT-
0901EC231118
Day3
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
t = 0:T:1-T; % Time vector for 1 second
f = 50; % Frequency of the signal (Hz)
0901EC231118
Day 3
% Plot Time Domain Signals
figure;
subplot(2, 1, 1);
plot(t, x, 'b', 'LineWidth', 1.5);
hold on;
plot(t_shifted, x_shifted_time, 'r', 'LineWidth', 1.5);
title('Time Domain: Original and Time Shifting');
xlabel('Time (seconds)');
ylabel('Amplitude');
legend('Original Signal', 'Shifted in Time');
0901EC231118
Day
Day3
Sampling
% Parameters
Fs_cont = 1000; % Continuous signal sampling frequency (Hz)
Fs_samp = 500; % Sampling frequency (Hz) for sampling below Nyquist
Fs_samp_high = 1200; % Sampling frequency (Hz) for sampling above Nyquist
T = 1/Fs_cont; % Sampling period for continuous signal (high)
t_cont = 0:T:1-T; % Time vector for continuous signal (1 second duration)
0901EC231118
x_cont = sin(2*pi*f_signal*t_cont); % Continuous time signal
Day 3
% Plot Continuous Signal and Sampled Signals
subplot(2, 1, 1);
plot(t_cont, x_cont, 'b', 'LineWidth', 1.5); hold on;
stem(t_samp, x_samp, 'r', 'LineWidth', 1.5);
title('Signal Sampling - Below Nyquist');
xlabel('Time (seconds)');
ylabel('Amplitude');
legend('Continuous Signal', 'Sampled Signal (Below Nyquist)');
grid on;
0901EC231118
subplot(2, 1, 2);
Day 3
0901EC231118
Day4
MATLAB Code to calculate dft using equation not using fft() function
% Example signal parameters
Fs = 1000; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 1000; % Length of the signal
t = (0:L-1)*T; % Time vector
for k = 0:N-1
X_dft(k+1) = sum(x .* exp(-1j * 2 * pi * k * (0:N-1) / N)); % DFT formula end
% Frequency vector
f = Fs*(0:(N/2))/N;
0901EC231118
ylabel('|X[k]|');
Day 4
% Phase Spectrum
phase_spectrum = angle(X_dft); % Phase of the DFT result (full spectrum)
Day 4
MATLAB Code for signal conversion from time domain to frequency domain
% Example signal parameters
Fs = 1000; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 1000; % Length of the signal
t = (0:L-1)*T; % Time vector
% Perform FFT
Y = fft(signal);
% Frequency vector
f = Fs*(0:(L/2))/L;
Day 4
0901EC231118
Day4
MATLAB Code to plot magnitude and phase spectrum of a signal in frequency domain
% Example signal parameters
Fs = 1000; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 1000; % Length of the signal
t = (0:L-1)*T; % Time vector
% Perform FFT
Y = fft(signal);
% Frequency vector
f = Fs*(0:(L/2))/L;
0901EC231118
ylabel('|P1(f)|');
Day 4
Day 5
Power Spectral Density (PSD) is a measure of how the power of a signal is distributed across
different frequency components. It represents the power content of a signal as a function of
frequency, providing insight into the signal's frequency content and energy distribution over
the spectrum. PSD is particularly useful in signal processing and communications to analyze
the frequency characteristics of a signal, noise, or any random process.
For discrete signals, the PSD can be computed as the average of the squared magnitudes of
the signal's Fourier coefficients.
Mathematically, the PSD is given by the square of the magnitude of the Fourier transform of
the signal, normalized by the signal length (or total time duration) to get a distribution per
unit frequency.
0901EC231118
Where:
X(f) is the Fourier transform of the signal,
f is the frequency variable, and
T is the duration of the signal.
Day 5
MATLAB Code to calculate Power spectral density distribution
% Example Signal Generation
Fs = 1000; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 1000; % Length of the signal
t = (0:L-1)*T; % Time vector
0901EC231118
subplot(3,1,1);
plot(t, x);
title('Time Domain Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
Day 5
% Plot the phase spectrum
phase_spectrum = angle(X); % Compute the phase of the FFT
subplot(3,1,3);
plot(f, phase_spectrum(1:N/2+1)); % Only plot the relevant part
title('Phase Spectrum');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
0901EC231118
Day 5
Spectogram
A spectrogram is a time-frequency representation of a signal that provides insight into how
its frequency content varies over time. It is a 2D plot that shows the amplitude (or power) of
different frequency components of a signal as they evolve over time. Essentially, a
spectrogram provides a visual representation of the time-varying spectrum of a signal.
A spectrogram is computed by dividing the signal into small segments (windows), typically
using a sliding window approach, and applying the Fourier Transform to each segment. This
results in a matrix where each column represents the frequency content of the signal at a
particular time instant.
How is a Spectrogram Useful in Time-Frequency Analysis?
The spectrogram is particularly useful in time-frequency analysis because it allows the
following:
Tracking Frequency Changes: It shows how the frequency content of a signal
changes over time, making it useful for non-stationary signals where the frequency
content varies (e.g., speech, music, or transient events).
Visualizing Non-Stationary Signals: Many real-world signals are non-stationary,
meaning their frequency content changes over time. The spectrogram captures this
time-varying frequency content.
Analyzing Transients and Events: It helps in detecting and analyzing transient
events or sudden changes in frequency, which is important in fields like audio
analysis, communications, and biomedical signal processing (e.g., EEG, ECG).
Noise and Signal Separation: It can be used to distinguish between noise and signal
by observing how the frequency content behaves over time.
Code
0901EC231118
f1 = 50; % Initial frequency of the sine wave (Hz)
f2 = 150; % Final frequency of the sine wave (Hz)
x = sin(2*pi*f1*t) + sin(2*pi*f2*(t+1)); % Signal with changing frequencies
Day 5
% Number of windows
num_windows = floor((L - window_size) / (window_size - overlap)) + 1;
0901EC231118
% Store the magnitude squared (Power Spectrum)
S(:, i) = abs(X).^2; end
Day 5
Day 5
MATLAB Code to show fft and spectrogram of a EEG/ECG signal.
0901EC231118
% Simulation parameters
Fs = 500; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 5000; % Length of the signal (10 seconds of ECG)
t = (0:L-1)*T; % Time vector
Day 5
% Spectrogram Calculation (Time-Frequency Analysis)
window_size = 256; % Length of the window (samples)
overlap = 128; % Overlap between windows (samples)
0901EC231118
nfft = 512; % Number of FFT points
subplot(3,1,3);
xlabel('Time (seconds)');
ylabel('Frequency (Hz)');
y = filter(b, a, x)
b is the numerator coefficient vector (or the feedforward coefficients) of the filter.
a is the denominator coefficient vector (or the feedback coefficients) of the filter.
x is the input signal to be filtered.
y is the output signal (filtered version of x).
In simple terms, filter() processes the input signal x with the defined filter (described by b
and a), producing an output y.
2. filtfilt()
0901EC231118
The filtfilt() function in MATLAB performs a zero-phase filtering operation by applying the
filter forwards and then backwards. This helps remove phase distortion. It is particularly
useful when you want to preserve the shape of the signal and avoid any phase shift.
The syntax is:
matlab
y = filtfilt(b, a, x)
Similar to filter(), b and a are the filter coefficients, and x is the input signal.
The difference with filter() is that filtfilt() applies the filter in both directions (forward
and reverse), resulting in a signal with no phase shift but still preserving the
magnitude response.
This function is useful when you want to minimize phase distortion while filtering your
signal.
3. freqz()
The freqz() function is used to compute and plot the frequency response of a digital filter. It
shows how the filter will affect the frequencies in a signal. It's especially useful when you
want to visualize the magnitude and phase response of the filter.
The syntax is:
matlab
[H, W] = freqz(b, a, N)
b and a are the filter coefficients (numerator and denominator).
N is the number of points used for plotting the frequency response (if not specified, a
default value is used).
H is the frequency response (complex values).
0901EC231118
N is the order of the filter (number of taps minus one).
Wn is the cutoff frequency (or frequencies) normalized between 0 and 1 (where 1 is
half the sampling rate).
'type' specifies the filter type, like 'low', 'high', 'bandpass', or 'stop'.
window (optional) specifies the windowing function to use (like Hamming, Hanning,
etc.) to design the filter.
For example, to design a low-pass FIR filter with a cutoff frequency of 0.2 (normalized):
Matlab
0901EC231118
Day 6 and Day 7
MATLAB Code to design a filter and apply on the signal for Noise reduction
% 1. Generate a clean signal (sine wave) and add noise
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1-1/fs; % Time vector (1 second duration)
f_clean = 50; % Frequency of the clean signal (Hz)
clean_signal = sin(2*pi*f_clean*t); % Clean sine wave signal
0901EC231118
noise = 0.5 * randn(size(t)); % Gaussian white noise
noisy_signal = clean_signal + noise; % Noisy signal
subplot(3,1,2);
plot(t, noisy_signal);
title('Noisy Signal');
xlabel('Time (s)');
ylabel('Amplitude');
0901EC231118