Azzera filtri
Azzera filtri

Compute A Moving Average on a Live Streamed Signal

16 visualizzazioni (ultimi 30 giorni)
Anas Khan
Anas Khan il 10 Mar 2022
Risposto: Shreeya il 7 Dic 2023
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
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.

Risposte (1)

Shreeya il 7 Dic 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
windowSize = 4;
sensorValues = [];
sum = 0;
function obj = MovingAverageFilter (windowSize)
if nargin == 1
obj.windowSize = windowSize;
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) = [];
result = obj.sum/length(obj.sensorValues);


Scopri di più su Signal Generation and Preprocessing in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by