MCPcopy Create free account
hub / github.com/brainflow-dev/brainflow / detrend

Function detrend

src/data_handler/data_handler.cpp:1180–1236  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1178}
1179
1180int detrend (double *data, int data_len, int detrend_operation)
1181{
1182 if ((data == NULL) || (data_len < 1))
1183 {
1184 data_logger->error (
1185 "Incorrect Data arguments. Data must not be empty and data_len must be >=1");
1186 return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
1187 }
1188 if (detrend_operation == (int)DetrendOperations::NO_DETREND)
1189 {
1190 return (int)BrainFlowExitCodes::STATUS_OK;
1191 }
1192 if (detrend_operation == (int)DetrendOperations::CONSTANT)
1193 {
1194 double mean = 0.0;
1195 // subtract mean from data
1196 for (int i = 0; i < data_len; i++)
1197 {
1198 mean += data[i];
1199 }
1200 mean /= data_len;
1201 for (int i = 0; i < data_len; i++)
1202 {
1203 data[i] -= mean;
1204 }
1205 return (int)BrainFlowExitCodes::STATUS_OK;
1206 }
1207 if (detrend_operation == (int)DetrendOperations::LINEAR)
1208 {
1209 // use mean and gradient to find a line
1210 double mean_x = (data_len - 1) / 2.0;
1211 double mean_y = 0;
1212 for (int i = 0; i < data_len; i++)
1213 {
1214 mean_y += data[i];
1215 }
1216 mean_y /= data_len;
1217 double temp_xy = 0.0;
1218 double temp_xx = 0.0;
1219 for (int i = 0; i < data_len; i++)
1220 {
1221 temp_xy += i * data[i];
1222 temp_xx += i * i;
1223 }
1224 double s_xy = temp_xy / data_len - mean_x * mean_y;
1225 double s_xx = temp_xx / data_len - mean_x * mean_x;
1226 double grad = s_xy / s_xx;
1227 double y_int = mean_y - grad * mean_x;
1228 for (int i = 0; i < data_len; i++)
1229 {
1230 data[i] = data[i] - (grad * i + y_int);
1231 }
1232 return (int)BrainFlowExitCodes::STATUS_OK;
1233 }
1234 data_logger->error ("Detrend operation is incorrect. Detrend:{}", detrend_operation);
1235 return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
1236}
1237

Callers 4

get_custom_band_powersFunction · 0.70
get_oxygen_levelFunction · 0.70
detrendMethod · 0.50
mainFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected