0% found this document useful (0 votes)
86 views2 pages

Matlab Code Emg

The MATLAB code simulates and processes an EMG signal, including the generation of synthetic EMG activity with noise and baseline wander. It applies band-pass and notch filtering, followed by full-wave rectification and linear envelope extraction. Finally, the code visualizes the raw EMG, filtered EMG, frequency spectrum, rectified EMG, and the EMG linear envelope in a series of plots.

Uploaded by

dhanuchinnu50
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)
86 views2 pages

Matlab Code Emg

The MATLAB code simulates and processes an EMG signal, including the generation of synthetic EMG activity with noise and baseline wander. It applies band-pass and notch filtering, followed by full-wave rectification and linear envelope extraction. Finally, the code visualizes the raw EMG, filtered EMG, frequency spectrum, rectified EMG, and the EMG linear envelope in a series of plots.

Uploaded by

dhanuchinnu50
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

MATLAB CODE:

clc;clear;
Fs = 1000;
T = 5;
t = 0:1/Fs:T-1/Fs;
emg_activity_burst = [zeros(1, 1.5*Fs),...
0.5 * sin(2*pi*50*t(1.5*Fs+1:2.5*Fs)) .* exp(-
5*t(1.5*Fs+1:2.5*Fs)),...
1.0 * sin(2*pi*80*t(2.5*Fs+1:3.5*Fs)) .* (1 - exp(-
10*(t(2.5*Fs+1:3.5*Fs)-t(2.5*Fs+1)))),...
0.3 * randn(1, 1.5*Fs)];
emg_activity_burst = emg_activity_burst(1:length(t));
noise_hf = 0.1 * randn(size(t));
noise_powerline = 0.2 * sin(2*pi*50*t);
raw_emg = emg_activity_burst + noise_hf + noise_powerline;
baseline_wander = 0.05 * sin(2*pi*0.5*t);
raw_emg = raw_emg + baseline_wander;

%Band-pass Filtering and Notch Filtering

f_highpass = 20;
f_lowpass = 450;
filter_order = 4;

[b_bandpass, a_bandpass] = butter(filter_order, [f_highpass/(Fs/2),


f_lowpass/(Fs/2)], 'bandpass');
filtered_emg = filtfilt(b_bandpass, a_bandpass, raw_emg);

f_notch = 50;
bandwidth_notch = 2;
[b_notch, a_notch] = iirnotch(f_notch/(Fs/2), bandwidth_notch/(Fs/2));
filtered_emg = filtfilt(b_notch, a_notch, filtered_emg);
%% Rectification (Full-wave)
rectified_emg = abs(filtered_emg);

% Smoothing (Linear Envelope Extraction)

smoothing_cutoff_freq = 6;
[b_envelope, a_envelope] = butter(filter_order, smoothing_cutoff_freq/(Fs/2),
'low');
emg_envelope_lp = filtfilt(b_envelope, a_envelope, rectified_emg);

% window_length_ms = 100;
% window_length_samples = round(window_length_ms / 1000 * Fs
% );
% if mod(window_length_samples, 2) == 0
% window_length_samples = window_length_samples + 1;
% end
% emg_envelope_rms = sqrt(movmean(rectified_emg.^2, window_length_samples));

%% Visualization

L = length(filtered_emg);
NFFT = 2^nextpow2(L);
Y = fft(filtered_emg, NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure(1);
subplot(5,1,1);
plot(t, raw_emg);
title('1. Raw EMG Signal');
xlabel('Time (s)');
ylabel('Amplitude (V)');
grid on;

subplot(5,1,2);
plot(t, filtered_emg);
title(['2. Band-pass (', num2str(f_highpass), '-', num2str(f_lowpass), ' Hz) &
50Hz Notch Filtered EMG']);
xlabel('Time (s)');
ylabel('Amplitude (V)');
grid on;

subplot(5,1,3);
plot(f, 2*abs(Y(1:NFFT/2+1)));
title('Frequency Spectrum of Filtered EMG');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
xlim([0 Fs/2]);
grid on;

subplot(5,1,4);
plot(t, rectified_emg);
title('3. Full-wave Rectified EMG');
xlabel('Time (s)');
ylabel('Amplitude (V)');
grid on;

subplot(5,1,5);
plot(t, emg_envelope_lp);
title(['4a. EMG Linear Envelope (Low-pass Filtered, Cutoff: ',
num2str(smoothing_cutoff_freq), ' Hz)']);
xlabel('Time (s)');
ylabel('Amplitude (V)');
grid on;

sgtitle('EMG Signal Processing: Rectification and Smoothing');


linkaxes([subplot(5,1,1) subplot(5,1,2) subplot(5,1,4) subplot(5,1,5)], 'x');
xlim([0.5 4]);

You might also like