public List <Sound> Calculate(WavData wavData) { var frequencies = new List <Sound>(); for (int i = 0; i < wavData.ChunkedSamples.Length; i++) { var samples = wavData.ChunkedSamples[i]; var reducedSamples = FourierHelper.ReduceToPow2(samples); reducedSamples = FourierHelper.PreEmphasis(reducedSamples); reducedSamples = _fourierWindow.Windowing(reducedSamples); var complexSamples = FourierTransform.FFT(reducedSamples); complexSamples = complexSamples.Take(complexSamples.Length / 2).ToArray(); var threshold = complexSamples.Max(c => c.Magnitude) / 10; var localMax = 0; for (int j = 1; j < complexSamples.Length; j++) { if (complexSamples[j].Magnitude > threshold && complexSamples[j].Magnitude > complexSamples[j - 1].Magnitude && complexSamples[j].Magnitude > complexSamples[j + 1].Magnitude) { localMax = j; break; } } var frequency = (wavData.FormatChunk.SampleRate / (complexSamples.Length * 2)) * localMax; if (frequencies.Any() && frequencies.Last().Frequency == frequency) { frequencies.Last().EndTime += 50; } else { frequencies.Add(new Sound { StartTime = i * 50, EndTime = i * 50 + 50, Frequency = frequency, Result = complexSamples.Select(c => c.Magnitude).ToArray() }); } } return(frequencies); }
public double[] FilterData(WavData wavData) { var result = new double[wavData.Samples.Length + _filterLength - 1]; var filterFactors = BasicFilter.LowPassFilterFactors(_cutFreq, wavData.FormatChunk.SampleRate, _filterLength); var filtered = _window.Windowing(filterFactors); var data = wavData.Samples.ToList(); var zeros = new double[_filterLength - 1]; data.InsertRange(0, zeros); data.AddRange(zeros); for (int i = _filterLength - 1; i < data.Count; i++) { for (int j = 0; j < filtered.Length; j++) { result[i - _filterLength + 1] += data[i - j] * filtered[j]; } } return(result); }