| 882 | } |
| 883 | |
| 884 | int get_psd (double *data, int data_len, int sampling_rate, int window_function, |
| 885 | double *output_ampl, double *output_freq) |
| 886 | { |
| 887 | if ((data == NULL) || (sampling_rate < 1) || (data_len < 1) || (data_len % 2 == 1) || |
| 888 | (output_ampl == NULL) || (output_freq == NULL)) |
| 889 | { |
| 890 | data_logger->error ("Please check to make sure all arguments aren't empty, sampling rate " |
| 891 | "is >=1 and data_len is even."); |
| 892 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 893 | } |
| 894 | double *re = new double[data_len / 2 + 1]; |
| 895 | double *im = new double[data_len / 2 + 1]; |
| 896 | int res = perform_fft (data, data_len, window_function, re, im); |
| 897 | if (res != (int)BrainFlowExitCodes::STATUS_OK) |
| 898 | { |
| 899 | delete[] re; |
| 900 | delete[] im; |
| 901 | return res; |
| 902 | } |
| 903 | double freq_res = (double)sampling_rate / (double)data_len; |
| 904 | for (int i = 0; i < data_len / 2 + 1; i++) |
| 905 | { |
| 906 | // https://www.mathworks.com/help/signal/ug/power-spectral-density-estimates-using-fft.html |
| 907 | output_ampl[i] = (re[i] * re[i] + im[i] * im[i]) / ((double)(sampling_rate * data_len)); |
| 908 | if ((i != 0) && (i != data_len / 2)) |
| 909 | { |
| 910 | output_ampl[i] *= 2; |
| 911 | } |
| 912 | output_freq[i] = i * freq_res; |
| 913 | } |
| 914 | delete[] re; |
| 915 | delete[] im; |
| 916 | return (int)BrainFlowExitCodes::STATUS_OK; |
| 917 | } |
| 918 | |
| 919 | int get_band_power (double *ampl, double *freq, int data_len, double freq_start, double freq_end, |
| 920 | double *band_power) |
no test coverage detected