フィルターのクリア

Compute A Moving Average on a Live Streamed Signal

10 ビュー (過去 30 日間)
Anas Khan
Anas Khan 2022 年 3 月 10 日
回答済み: Shreeya 2023 年 12 月 7 日
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.

回答 (1 件)

Shreeya
Shreeya 2023 年 12 月 7 日
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

カテゴリ

Help Center および File ExchangeSignal Generation and Preprocessing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by