private Harmonic GetHarmonicAmplitudeAndPhase(double [] funcValues, int harmonicNumber) { double re = 0; double im = 0; int sinIndex = 0; int cosIndex = cosOffset; for (int i = 0; i < samplingFrequency; i++) { re += funcValues[i] * sinArray[cosIndex]; im += funcValues[i] * sinArray[sinIndex]; sinIndex = (sinIndex + harmonicNumber) % samplingFrequency; cosIndex = (cosIndex + harmonicNumber) % samplingFrequency; } double amplitudeCos = 2 * re / samplingFrequency; double amplitudeSin = 2 * im / samplingFrequency; double amplitude = Math.Sqrt(Math.Pow(amplitudeCos, 2) + Math.Pow(amplitudeSin, 2)); double phase = Math.Atan2(amplitudeSin, amplitudeCos); amplitude = Math.Round(amplitude, DigitsAfterDot); phase = Math.Round(phase, DigitsAfterDot); Harmonic result = new Harmonic(harmonicNumber, amplitude, phase); return(result); }
/// <summary> /// Get Amplitude Frequency and Phase Frequency response. /// </summary> /// <param name="funcValues">Array of values from what should be Amplitude and Phase spectrums created.</param> /// <param name="countOfHarmonics">The number of harmonics into which <paramref name="funcValues"/> should be divided.</param> /// <returns>Array of <see cref="Harmonic"/> where each element contains Amplitude and Phase of a harmonic.</returns> /// <exception cref="ArgumentException">if the length of <see cref="funcValues"/> is not equal to Sampling Frequency.</exception> public Harmonic[] GetFrequencyResponse(double[] funcValues, int countOfHarmonics /*, FrequencyResponse responseType*/) { if (funcValues.Length < samplingFrequency) { throw new ArgumentException(nameof(funcValues), $"Size of the array should be equal or greater than {samplingFrequency}"); } Harmonic[] result = new Harmonic[countOfHarmonics]; for (int i = 0; i < countOfHarmonics; i++) { result[i] = GetHarmonicAmplitudeAndPhase(funcValues, i); } return(result); }
private Harmonic[] ParseComplex(Complex[] array) { int halfFrequencyArray = array.Length / 2; Harmonic[] result = new Harmonic[halfFrequencyArray]; double amplitude = array[0].Magnitude / samplingFrequency; double phase = 0; result[0] = new Harmonic(0, amplitude, phase); for (int i = 0; i < halfFrequencyArray; i++) { amplitude = Math.Round(array[i].Magnitude / halfSamplingFrequency, DigitsAfterDot); phase = Math.Round(Math.Atan2(array[i].Imaginary, array[i].Real), DigitsAfterDot); result[i] = new Harmonic(i, amplitude, phase); } return(result); }