Compute A Moving Average on a Live Streamed Signal
4 views (last 30 days)
Show older comments
I am live streaming an EMG signal from the OpenBCI_GUI (using the Ganglion board) to MatLab using LabStreamingLayer. The basic code for receiving the stream involves a while loop and continuously refreshing the value of the variable "vec". Shown below:
%% instantiate the library
disp('Loading the library...');
lib = lsl_loadlib();
% resolve a stream...
disp('Resolving an EEG stream...');
result = {};
while isempty(result)
result = lsl_resolve_byprop(lib,'type','EEG'); end
% create a new inlet
disp('Opening an inlet...');
inlet = lsl_inlet(result{1});
disp('Now receiving data...');
while true
% get data from the inlet
[vec,ts] = inlet.pull_sample();
% and display it
fprintf('%.2f\t',vec);
fprintf('%.5f\n',ts);
end
There are some resources from MathWorks talking about System Objects and using them for streaming data by sectioning off the data into frames and analyzing a frame at a time. I could not find anything concrete for my use though. What I need to do is have a relatively efficient algorithm to real-time convert the streamed EMG signal into a smoothed signal using the moving average and then write it out so that my Arduino board can interpert the control signal. I thought that somehow storing a frame at a time to process might be a good option, but I'm not exactly sure how this would work when trying to write the streamed data to a new vector for the Arduino.
0 Comments
Answers (1)
Shreeya
on 7 Dec 2023
I understand that you want to implement an efficient moving average filter for a livestream of data and further write it on an Arduino board.
The class “MovingAverageFilter” implemented below can help achieve this. The underlying idea is to define a window size and maintain a FIFO queue, such that a pop operation is performed when the queue capacity exceeds the window size. The average is maintained for all the values currently present in the queue.
classdef MovingAverageFilter < handle
properties
windowSize = 4;
sensorValues = [];
sum = 0;
end
methods
function obj = MovingAverageFilter (windowSize)
if nargin == 1
obj.windowSize = windowSize;
end
end
function result = movingAverageCalculation(obj,value)
obj.sensorValues = [obj.sensorValues, value];
obj.sum = obj.sum + value;
if(length(obj.sensorValues) > obj.windowSize)
obj.sum = obj.sum - obj.sensorValues(1);
obj.sensorValues(1) = [];
end
result = obj.sum/length(obj.sensorValues);
end
end
end
0 Comments
See Also
Categories
Find more on Signal Generation and Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!