detect troughs (minimum) of a signal to fire an action. (the signal is being acquired online)
2 views (last 30 days)
Show older comments
Hi.. I am acquiring a signal from an EEG. I know approximatly the frequency of the signal.
I need to play a sound when the signal is in its minimum ( a trough ). So I need to detect the correct instant to fire the audio in real ltime.
I saw some functions as findpeaks but if I understood it correctly, it needs a store signal to work and it is not used in real time detection. Is that right?
Is there any tool boxes in signal processing that I could use for this taks.?
Tks
2 Comments
Image Analyst
on 3 Nov 2023
How would you know any particular point is in a trough? How could you predict the future to know that it won't continue lower, deeper into the trough and that it's not early in the trough or on the side of it? Could you just set a threshold somehow and if the signal falls below that threshold, it's considered to be a trough, or at least entering a trough even if it's not at the very bottom of it?
Accepted Answer
Pratyush Swain
on 16 Nov 2023
Hi L,
I understand you have a mechanism to receive signal values in real time and you want to detect troughs in it. The function 'findpeaks' is not feasible since it needs a stored signal as its input. We can incorporate window approach for handling realtime signal data.
Please refer an example implementation as follows:
% Set the sampling frequency and approximate frequency of the EEG signal
fs = 1000; % Sample rate in Hz
approximateFrequency = 10; % Set Approximate frequency of the EEG signal in Hz
% Set the window size for the smoothing filter and derivative calculation.
% Ensure this window size is more than the length of received signal from online
windowSize = round(fs / approximateFrequency); % Adjust this value as needed
% Set the threshold for trough detection
threshold = -0.5; % Adjust this value based on the characteristics of your EEG signal
% Initialize variables
eegSignal = zeros(windowSize, 1);
troughDetected = false;
% Main loop for real-time processing
while true
% Acquire the EEG signal (replace with your specific code)
newSamples = acquireEEGSignal(); % Replace with your code to acquire the EEG signal
% Handle multiple samples returned by acquireEEGSignal
for i = 1:numel(newSamples)
% Update the EEG signal buffer
eegSignal = circshift(eegSignal, -1); %takes the first element of arr to end of arr
eegSignal(end) = newSamples(i); %substitute value at the end of arr with new value
% Apply a smoothing filter to the EEG signal for noisy fluctuating
% data
smoothedSignal = smoothdata(eegSignal, 'movmean', windowSize/3);
% Calculate the first derivative of the smoothed signal
derivative = diff(smoothedSignal);
% Check if the derivative goes from negative to positive
if derivative(end-1) < 0 && derivative(end) > 0 && ~troughDetected
% Play sound or perform action at the detected trough
playSound(); % Replace with your code to play sound or perform desired action
% Set trough detected flag to prevent repeated triggering
troughDetected = true;
end
% Reset the trough detected flag if the derivative goes below the threshold
if derivative(end) < threshold
troughDetected = false;
end
end
% Add a delay to control the loop rate
pause(0.01); % Adjust the delay time as needed
end
Please note the 'threshold' value signifies a slope/derivative value and helps prevent false triggers by requiring the derivative to fall below a certain threshold before considering the detection of a new trough.This mechanism ensures that only significant troughs are detected and helps filter out small fluctuations or noise in the EEG signal.
Hope this helps.
0 Comments
More Answers (0)
See Also
Categories
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!