private List <double> RemoveBestPhaseShiftedCycle(List <double> Data, out ITransformation Transform, out double Error) { double[] cycle = GetCycle((Double[])Data.ToArray()); for (int phase = (_maxCycleDelay * -1); phase < _maxCycleDelay; phase++) { List <double> decycledData = new List <double>(); double lastX = 0.0; for (int i = 0; i < Data.Count; i++) { if (((i + phase) < cycle.Length) && ((i + phase) >= 0)) { decycledData.Add(Data[i] - cycle[i + phase]); lastX = i + phase; } } if (decycledData.Count < _minLength) { continue; } _phaseShiftScoringStrategy.StartNewTransformation(Data, new EhlerTransformation(cycle, (int)lastX)); int decycleIndex = 0; for (int i = 0; i < Data.Count; i++) { if (((i + phase) < cycle.Length) && ((i + phase) >= 0)) { _phaseShiftScoringStrategy.AddTransformedValue(decycledData[decycleIndex++]); } } _phaseShiftScoringStrategy.Score(); } Error = _phaseShiftScoringStrategy.LowestError; Transform = _phaseShiftScoringStrategy.BestTransform; return(_phaseShiftScoringStrategy.BestTransformedData); }
public List <double> Remove( List <double> Data, ref Stack <ITransformation> Transforms) { if (Transforms == null) { Transforms = new Stack <ITransformation>(); } var n = Data.Count; if (_fourierType == FourierType.FFT && ((n & (n - 1)) != 0)) { throw new ApplicationException("for FFT data size needs to be a power of two"); } List <Wave> waves = null; double[] t; DoFourier(Data, out waves, out t); double[] mags = new double[waves.Count]; for (int i = 0; i < waves.Count; i++) { Wave wave = waves[i]; mags[i] = (int)Math.Floor(wave.Magnitude); } double stdDevOfMags = Utilities.StandardDeviation(new List <double>(mags)); _scoringStrategy.StartNewTransformation(Data, null); List <double> decycledData = (List <double>)Utilities.DeepClone(Data); List <double> previousDecycledData = (List <double>)Utilities.DeepClone(decycledData); double lowestError = 0.0; double[] compositeWaveValues = new double[Data.Count]; int indexOfLowest = 0; for (int i = 0; i < waves.Count; i++) { double frequency = waves[i].Frequency; double magnitude = waves[i].Magnitude; double phase = waves[i].Phase; //if (magnitude / stdDevOfMags < _spectrumPeakStdDevs) // break; for (int j = 0; j < decycledData.Count; j++) { compositeWaveValues[j] += Math.Cos((2 * Math.PI * frequency * t[j]) + phase) * magnitude; decycledData[j] -= compositeWaveValues[j]; _scoringStrategy.AddTransformedValue(Data[j] - compositeWaveValues[j]); } _scoringStrategy.Score(); double error = _scoringStrategy.LowestError; if (error < lowestError) { lowestError = error; indexOfLowest = i; decycledData = (List <double>)Utilities.DeepClone(Data); } } compositeWaveValues = new double[Data.Count]; CompositeWave compositeWave = new CompositeWave(WaveType.Cosine); double lastX = 0.0; for (int i = 0; i <= indexOfLowest; i++) { double frequency = waves[i].Frequency; double magnitude = waves[i].Magnitude; double phase = waves[i].Phase; for (int j = 0; j < decycledData.Count; j++) { compositeWaveValues[j] += Math.Cos((2 * Math.PI * frequency * t[j]) + phase) * magnitude; decycledData[j] -= (compositeWaveValues[j] * _filterWindowForResults.ValueAt(decycledData.Count, j)); lastX = t[j]; } compositeWave.Add(new Wave(WaveType.Cosine, frequency, magnitude, phase)); } Transforms.Push(new WaveTransformation(compositeWave, lastX)); #if DEBUG StringBuilder sbCompDecycle = new StringBuilder(); for (int i = 0; i < compositeWaveValues.Length; i++) { double comp = compositeWaveValues[i] * _filterWindowForResults.ValueAt(decycledData.Count, i); double decycle = decycledData[i]; sbCompDecycle.AppendLine(String.Format("{0},{1}", comp, decycle)); } #endif return(decycledData); }