Bio Assingnment 1 Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

BIO-MEDICAL SIGNAL PROCESSING

ASSIGNMENT – 1

- Thatukuru Lakshman
- CB.EN.U4AIE22067

6) Aliasing Demonstration in Matlab

clc;

clear all;

close all;

samplingFrequency = 50;

signalFrequency = 10;

frequencyShiftFactor = 100;

shiftedFrequency = signalFrequency + frequencyShiftFactor *


samplingFrequency;

timeVector = 0:1/samplingFrequency:1;

signal1 = sin(2 * pi * signalFrequency * timeVector);

signal2 = sin(2 * pi * shiftedFrequency * timeVector);

figure;

subplot(2, 1, 1);

stem(timeVector, signal1, 'filled');

title('Signal with original frequency');

subplot(2, 1, 2);

stem(timeVector, signal2, 'filled');

title('Signal with shifted frequency');


OUTPUT

7) Demonstration of Power Spectrum and Power Spectral Density(PSD) in


Matlab

clc;

clear all;

close all;

fs = 500;

N = 1000;

x = 0:N-1;

t = x * (1/fs);
y = 1 * sin(2 * pi * 100 * t) + 4 * sin(2 * pi * 110 * t);

Y = fft(y);

Power_spectrum = (abs(Y) / N).^2;

f = (0:N-1) * (fs / N);

Power_spectrum = Power_spectrum(1:N/2+1);

f = f(1:N/2+1);

PSD = Power_spectrum / N;

figure;

subplot(3,1,1);

plot(t, y);

title('Original Signal');

xlabel('Time (s)');

ylabel('Amplitude');

grid on;

subplot(3,1,2);

plot(f, 10*log10(Power_spectrum));

title("Power Spectrum of the Signal");

xlabel('Frequency (Hz)');

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

grid on;

subplot(3,1,3);

plot(f, 10*log10(PSD));

title("Power Spectral Density of the Signal");

xlabel('Frequency (Hz)');

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

grid on;
OUTPUT

9) Demonstration of a signal after Interpolation in Maltab

clc; clear all; close all;

fs = 50; % Original sampling frequency (Hz)

% Load the eeg data from a .mat file

data = load("/Users/lakshmanthatukuru/Desktop/eeg_data.mat");

y1 = data.eeg; % Extract the eeg data from the loaded structure

N = length(y1); % Number of samples in the original signal

t = (0:N-1)/fs; % Time vector for the original signal


new_fs = 100; % New (higher) sampling frequency for interpolation

t1 = t(1):1/new_fs:t(end); % Time vector for the interpolated signal

% Performing cubic spline interpolation

new_y = interp1(t, y1, t1, 'cubic');

% Plotting

figure;

% Original signal

subplot(2,1,1);

plot(t, y1, 'r');

title('Signal before interpolation');

xlabel('Time (s)');

ylabel('Amplitude');

% Interpolated signal

subplot(2,1,2);

plot(t1, new_y, 'b');

title('Signal after Interpolation');

xlabel('Time (s)');

ylabel('Amplitude (Interpolated)');

OUTPUT
10) The file heart_rate_sample contains 2 heart rates – hr_med and hr_pre
during two conditions with corresponding time vectors t_med and t_pre.
Both these signals are unevenly sampled. Convert the hr_pre to evenly
spaced data (resample) using interpolation. You can take the sampling
frequency as 100Hz

clc; clear all; close all;

% Load the heart rate data from a .mat file

data = load ("/Users/lakshmanthatukuru/Desktop/heart_rate_samples.mat");

hr_pre = data.hr_pre;

t_pre = data.t_pre;

t = t_pre

N = length(hr_pre) % Number of samples in the original signal

new_fs = 100; % New (higher) sampling frequency for interpolation

t1 = t(1):1/new_fs:t(end) ; % Time vector for the interpolated signal

% Performing cubic spline interpolation

new_y = interp1(t_pre, hr_pre, t1, 'cubic');


% Original signal

figure;

subplot(2,1,1);

plot(t, hr_pre, 'r');

title('Signal before interpolation');

xlabel('Time (s)');

ylabel('Amplitude');

% Interpolated signal

subplot(2,1,2);

plot(t1, new_y, 'b');

title('Signal after Interpolation');

xlabel('Time (s)');

ylabel('Amplitude (Interpolated)');

OUTPUT
11) Create a noisy signal to generate a waveform of 200,400 Hz in
MATLAB with SNR -8 dB, sampling frequency=1000Hz, and N=512
samples. Plot magnitude spectrum.

Repeat with -4dB, -16dB, plot the magnitude spectrums – write your
observation in detecting the signal frequency.
Repeat with N =1000 and N=200 samples, plot the magnitude spectrums -
and write the inference when detecting the signal frequency.

clc; clear; close all;

fs = 1000; % Sampling frequency in Hz

N = 512; % Number of samples

f1 = 200; % Frequency of the first sine wave in Hz

f2 = 400; % Frequency of the second sine wave in Hz

% Time vector

t = (0:N-1) / fs;

% Signal generation: sum of two sine waves

y = sin(2 * pi * f1 * t) + sin(2 * pi * f2 * t);

% SNR values to test

snr_values = [-4, -8];

% Plotting for different SNR values

figure;

for i = 1:length(snr_values)

snr = snr_values(i);

% Signal power

signal_power = mean(y .^ 2);

% Noise power calculation from SNR

noise_power = signal_power / (10 ^ (snr / 10));


% Generate Gaussian noise

noise = randn(size(y)) * sqrt(noise_power); % Noise with given power

% Add noise to the signal

signal = y + noise;

% FFT and normalization

fft_sign = abs(fft(signal) / N); % Normalize the FFT result

% Take the positive half of the spectrum

fft_sign = fft_sign(1:N/2+1);

f = (0:N/2) * (fs / N);

% Plot magnitude spectrum

subplot(2, 1, i);

plot(f, fft_sign, 'c');

xlabel('Frequency (Hz)');

ylabel('Magnitude');

title(['SNR = ', num2str(snr), ' dB, N = ', num2str(N)]);

end

% Repeat with different number of samples

N_values = [1000, 200];

for j = 1:length(N_values)

N = N_values(j);

t = (0:N-1) / fs;

y = sin(2 * pi * f1 * t) + sin(2 * pi * f2 * t);

figure;

for i = 1:length(snr_values)

snr = snr_values(i);

% Signal power
signal_power = mean(y .^ 2);

% Noise power calculation from SNR

noise_power = signal_power / (10 ^ (snr / 10));

% Generate Gaussian noise

noise = randn(size(y)) * sqrt(noise_power); % Noise with given


power

% Add noise to the signal

signal = y + noise;

% FFT and normalization

fft_sign = abs(fft(signal) / N); % Normalize the FFT result

% Take the positive half of the spectrum

fft_sign = fft_sign(1:N/2+1);

f = (0:N/2) * (fs / N);

% Plot magnitude spectrum

subplot(length(snr_values), 1, i);

plot(f, fft_sign, 'c');

xlabel('Frequency (Hz)');

ylabel('Magnitude');

title(['SNR = ', num2str(snr), ' dB, N = ', num2str(N)]);

end

end

OUTPUT
Figure 1:
Figure 2:
Figure 3:

The interpretation you've provided explains how variations in Signal-to-Noise


Ratio (SNR) and sample size (N) affect the clarity and resolution of signal peaks
in the frequency domain:

1. SNR Variation:
- Lower SNR values: When the SNR is low, the noise level is high relative to
the signal. This increased noise makes it more challenging to discern the true
signal components, resulting in less clear or visible peaks at specific frequencies
like 200 Hz and 400 Hz. The signal is overwhelmed by noise, which obscures
the important features of the signal.
- Higher SNR values: With a higher SNR, the signal stands out more clearly
above the noise. This makes the signal peaks at 200 Hz and 400 Hz more
distinguishable and easier to detect. The reduced noise interference allows for a
clearer representation of the signal in the frequency domain.

2. Sample Size Variation:


- Increasing the number of samples (N): A larger sample size improves the
frequency resolution of the signal. This means the frequency components of the
signal can be more precisely identified, leading to sharper and more distinct
peaks at 200 Hz and 400 Hz. The signal's frequency content is captured more
accurately, resulting in better-defined peaks.
- Fewer samples: With a smaller sample size, the frequency resolution
decreases. This causes the signal peaks to appear broader and less defined, as
there are fewer data points to accurately represent the frequency content. The
reduced sample size limits the ability to precisely identify the frequencies
present in the signal.

This interpretation highlights the importance of both SNR and sample size in
accurately analyzing signals, particularly in applications like signal processing
and spectral analysis..

You might also like