Record real-time EMG signal and Plot it.
70 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (2)
Aastha
il y a environ 23 heures
As I understand, you want to interface with the Arduino board and sample an EMG signal at a specified sampling rate using MATLAB. Further, you want to visualize the sampled signal as a function of the sampling time in milliseconds. You can do this by making the following modifications to the code:
Create a “dt” variable that stores the time between each sample. You may refer to the code snippet below to do so:
dt = 1 / sampling_frequency; % Time interval between samples in seconds
Next, create an array to hold the sampling times.
sampling_times_ms = zeros(1, num_samples); % array to store the sampling times
The break condition in the for loop is not required since you have already computed the “num_samples” variable based on the total sampling duration of 60 seconds.
Additionally, move the “pause” function outside the if condition. In the for loop, you can store the time at which each sample was recorded so that you can coordinate the recording and plotting times. This can be done in MATLAB as illustrated below:
tic; % Start the timer
for i = 1:num_samples
% Measure the elapsed time in milliseconds
elapsed_time = toc * 1000; % Convert seconds to milliseconds
sampling_times_ms(i) = elapsed_time;
x(i) = readVoltage(a, 'A0'); % read the voltage from the A0 pin
% Pause for the required sampling interval
pause(dt - (toc - elapsed_time / 1000)); % adjust the pause to account for delay
To plot the EMG signal, refer to the MATLAB code below:
plot(sampling_time_ms, x);
With these changes to the code, you should be able to better align the actual sampling times with both the recording and plotting times.
For any further information on the “pause” function, you may refer to MathWorks documentation whose link is mentioned below:
I hope this is helpful!
0 commentaires
Ruchika Parag
il y a environ 21 heures
Hi @Iro Liontou, to achieve precise timing and accurate plotting of your sEMG signal, consider the following key points:
- Loop Timing: Ensure each loop iteration takes exactly 1 ms for a 1 kHz sampling rate. Use tic and toc to measure and adjust the loop's execution time.
- Minimize Delays: Functions 'disp' and 'beep' can introduce delays. Use them sparingly or outside the loop if possible. Please refer to the following official MathWorks documentation to know more about the functions 'disp' and 'beep': https://www.mathworks.com/help/matlab/ref/disp.html
- Pause Adjustment: After each iteration, calculate the time taken and use 'pause' to compensate for any delay, ensuring consistent timing. Please refer to the following official MathWorks documentation to know more about the function 'pause': https://www.mathworks.com/help/matlab/ref/pause.html
- Real-Time Plotting: If needed, update the plot within the loop, but do so sparingly to avoid slowing down the loop.
This will help in maintaining a consistent sampling rate and ensures accurate timing for both acquisition and plotting.
0 commentaires
Voir également
Catégories
En savoir plus sur Hardware Discovery and Setup dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!