v2.0.0
Loading...
Searching...
No Matches
stim_artifact.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "stim_artifact.h"
18
19//=============================================================================================================
20// STL INCLUDES
21//=============================================================================================================
22
23#include <cmath>
24#include <algorithm>
25
26//=============================================================================================================
27// USED NAMESPACES
28//=============================================================================================================
29
30using namespace UTILSLIB;
31using namespace Eigen;
32
33//=============================================================================================================
34// DEFINE MEMBER METHODS
35//=============================================================================================================
36
37void UTILSLIB::fixStimArtifact(MatrixXd& data,
38 const MatrixXi& events,
39 double sfreq,
40 int eventId,
41 double tmin,
42 double tmax,
44{
45 if (events.rows() == 0 || events.cols() < 3 || data.cols() == 0 || data.rows() == 0) {
46 return;
47 }
48
49 const int iMinOffset = static_cast<int>(std::round(tmin * sfreq));
50 const int iMaxOffset = static_cast<int>(std::round(tmax * sfreq));
51 const int iNumCols = static_cast<int>(data.cols());
52 const int iNumRows = static_cast<int>(data.rows());
53
54 for (int e = 0; e < events.rows(); ++e) {
55 // Filter by event ID if requested
56 if (eventId >= 0 && events(e, 2) != eventId) {
57 continue;
58 }
59
60 // Compute window boundaries
61 int iStart = events(e, 0) + iMinOffset;
62 int iEnd = events(e, 0) + iMaxOffset;
63
64 // Clamp to data bounds
65 iStart = std::max(0, iStart);
66 iEnd = std::min(iNumCols - 1, iEnd);
67
68 if (iEnd <= iStart) {
69 continue;
70 }
71
72 const int iWindowLen = iEnd - iStart + 1;
73
74 switch (mode) {
76 for (int ch = 0; ch < iNumRows; ++ch) {
77 const double dStartVal = data(ch, iStart);
78 const double dEndVal = data(ch, iEnd);
79 for (int s = 0; s < iWindowLen; ++s) {
80 const double dAlpha = static_cast<double>(s) / static_cast<double>(iWindowLen - 1);
81 data(ch, iStart + s) = dStartVal + dAlpha * (dEndVal - dStartVal);
82 }
83 }
84 break;
85 }
87 data.block(0, iStart, iNumRows, iWindowLen).setZero();
88 break;
89 }
90 }
91 }
92}
Declaration of fixStimArtifact — stimulus artifact repair by interpolation or zeroing.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
StimArtifactMode
Mode for stimulus artifact repair.
DSPSHARED_EXPORT void fixStimArtifact(Eigen::MatrixXd &data, const Eigen::MatrixXi &events, double sfreq, int eventId=-1, double tmin=0.0, double tmax=0.01, StimArtifactMode mode=StimArtifactMode::Linear)
Repair stimulus artifacts by interpolating or zeroing data around events.