// calculate the power spectrum of the incoming signal stored in data field by estimating linear model and applying the lookupTable on the model public double[] estimatePowerSpectrum() { // init power spectrum double[] powerSpectrum = new double[numBins]; // if filter is constructed properly and data filed is filled with suitable data if (allowRun && data != null && linModel != null) { // init variables int counter = 0; // counter to fill powerspectrum array // multiply power by a factor of sqrt(2) to account for positive and negative frequencies. linModel = linModel * Math.Sqrt(2.0); // perform power spectrum estimation: cycle through bins, per bin calculate for each sample, ie frequencyresolution. (in BCI2000 this was transferspectrum.evaluate function, based loosely on function evlmem in Press et al.) for (int bin = 0; bin < numBins; ++bin) { // init powerSpectrum[bin] = 0.0; int evalInBin = (int)bins[bin, 2]; // cycle through samples per bin for (int eval = 0; eval < evalInBin; eval++) { // get power estimate for given frequency in lookupTable and store in power spectrum output array Complex valueComplex = linModel.evaluate(lookupTable[counter]); powerSpectrum[bin] += ((valueComplex.Real * valueComplex.Real) + (valueComplex.Imaginary * valueComplex.Imaginary)); // it's actually the squared magnitude rather than a norm counter++; } // normalize spectral power to frequency resolution powerSpectrum[bin] /= evalInBin; // divide by two and take square root to get amplitudes powerSpectrum[bin] = Math.Sqrt(powerSpectrum[bin] / 2); } // output power spectrum estimation //logger.Debug("Power spectrum estimation of data:"); //logger.Debug("Frequency bin \t Power"); //for (int i = 0; i < numBins; i++) { // double startBin = (firstBinCenter + (binWidth * i)) - (binWidth / 2); // double endBin = (firstBinCenter + (binWidth * i)) + (binWidth / 2); // logger.Debug("{0} - {1} Hz \t {2}", startBin, endBin, powerSpectrum[i]); //} } else { logger.Error("ARFilter is not constructed properly or input data is not suitable. Power spectrum can not be determined. See log files for more information."); } // return powerspectrum return(powerSpectrum); }