Record real-time EMG signal and Plot it.

47 visualizaciones (últimos 30 días)
Iro Liontou
Iro Liontou el 11 de Oct. de 2024
Editada: Walter Roberson el 11 de Oct. de 2024
Created this code :
%% Raw sEMG Signal Measuring with Precise Timing and X-Axis in Milliseconds
clc;
clear;
close all;
% Parameters
sampling_frequency = 1000; % Sampling frequency in Hz (1 kHz)
duration = 60; % Duration of signal acquisition in seconds
num_samples = sampling_frequency * duration; % Total number of samples
% Swallow timer setup
swallow_interval = 10; % Swallow every 10 seconds
next_swallow_time = swallow_interval; % Initial swallow time
% Initialize variables
x = zeros(num_samples, 1); % Array to store raw voltage data
time_ms = (0:num_samples-1) / sampling_frequency * 1000; % Time array for x-axis in milliseconds
% Connect to Arduino with higher baud rate (115200)
a = arduino('COM8', 'Uno', 'BaudRate', 115200); % Replace 'COMx' with your port
% Start acquisition and timing
start_time = tic; % Start the overall timer
% Start loop to acquire signal
for i = 1:num_samples
% Read voltage from analog pin A0
raw_voltage = readVoltage(a, 'A0');
% Store the raw voltage in the array
x(i) = raw_voltage;
% Calculate elapsed time
elapsed_time = toc(start_time);
% Swallow notification logic
if elapsed_time >= next_swallow_time
disp(['Time to swallow at t = ', num2str(next_swallow_time), ' seconds']);
next_swallow_time = next_swallow_time + swallow_interval; % Set next swallow time
% Optional: Play a sound or beep for notification
beep; % Beep sound (you can adjust with 'sound' for different notifications)
end
% Stop acquisition after 60 seconds
if elapsed_time >= duration
disp('Acquisition complete. Plotting the signal...');
break; % Exit the loop once 60 seconds have passed
end
% Measure how long this iteration took
iteration_time = toc(start_time) - elapsed_time;
% Compensate for any delay in the loop to keep the sampling frequency consistent
if iteration_time < (1 / sampling_frequency)
pause((1 / sampling_frequency) - iteration_time); % Adjust to match the sampling rate
end
end
% Cleanup: Clear Arduino object when done
clear a;
% Plot the raw sEMG signal after acquisition (in milliseconds)
figure('Name', 'Raw sEMG Signal After Acquisition');
plot(time_ms(1:i), x(1:i)); % Only plot up to the last acquired sample
grid on;
title('Raw sEMG Signal After Acquisition');
xlabel('Time (ms)');
ylabel('Voltage (V)');
xlim([0, duration * 1000]); % Adjust x-axis to show time in milliseconds
ylim([0, 5]); % Adjust based on your expected voltage range
What I want to achieve is that i record every 10 second a muscle contraction and after 60 seconds I plot the signal. The problem is that it plots the whole signal before the first 10 second. I tried the first time to do it real time like recording and plotting the signal in real time, but the seconds were not right.The x-axis was every 0.5 seconds but the recording lasted much longer. What is the problem here I don't understand what I really need to fix. Maybe it;s the loop that takes time or the readVoltage function of the MATLAB, but how do I coordinate both of the times, the real time recording and the one plotting?

Respuestas (0)

Categorías

Más información sobre Hardware Discovery and Setup en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by