void FourierBTClick(object sender, EventArgs e) { if (soundList.SelectedItems.Count == 0) { return; } if (waveWriter != null) { return; } string sFile = soundList.SelectedItem.ToString(); ComplexSignal sComplex = FFTComplex(sFile); Complex[] channel = sComplex.GetChannel(0); chart2.Series.Clear(); chart2.Series.Add("FFT"); chart2.Series["FFT"].ChartType = SeriesChartType.FastLine; chart2.Series["FFT"].ChartArea = "ChartArea1"; double[] power = FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(channel)); double[] freqv = FilterVoiceHz(Accord.Audio.Tools.GetFrequencyVector(sComplex.Length, sComplex.SampleRate)); for (int i = 1; i < power.Length; i++) { chart2.Series["FFT"].Points.AddXY(freqv[i], power[i]); } }
void source_NewFrame(object sender, NewFrameEventArgs eventArgs) { ComplexSignal signal = ComplexSignal.FromSignal(eventArgs.Signal); if (hammingWindowToolStripMenuItem.Checked) { ComplexSignal c = window.Apply(signal, 0); signal = c; } signal.ForwardFourierTransform(); Complex[] channel = signal.GetChannel(0); double[] power = Accord.Audio.Tools.GetPowerSpectrum(channel); double[] freqv = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate); power[0] = 0; // zero DC float[] g = new float[power.Length]; for (int i = 0; i < power.Length; i++) { // g[i, 0] = freqv[i]; g[i] = (float)power[i]; } chart1.RangeX = new DoubleRange(freqv[0], freqv[freqv.Length - 1] / hScrollBar1.Value); chart1.RangeY = new DoubleRange(0f, Math.Pow(10, -vScrollBar1.Value)); chart1.UpdateWaveform("fft", g); }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void source_NewFrame(object sender, NewFrameEventArgs eventArgs) { // We can start by converting the audio frame to a complex signal ComplexSignal signal = ComplexSignal.FromSignal(eventArgs.Signal); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. Complex[] channel = signal.GetChannel(0); double[] power = Accord.Audio.Tools.GetPowerSpectrum(channel); double[] freqv = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate); power[0] = 0; // zero DC float[] g = new float[power.Length]; for (int i = 0; i < power.Length; i++) { g[i] = (float)power[i]; } // Adjust the zoom according to the horizontal and vertical scrollbars. chart1.RangeX = new DoubleRange(freqv[0], freqv[freqv.Length - 1] / hScrollBar1.Value); chart1.RangeY = new DoubleRange(0f, Math.Pow(10, -vScrollBar1.Value)); chart1.UpdateWaveform("fft", g); }
public float[] EEGToSound(double[] inputData) { ComplexSignal cs = Signal.FromArray(inputData, SAMPLE_RATE).ToComplex(); cs.ForwardFourierTransform(); powerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0)); Array.ConstrainedCopy(powerSpectrum, 0, shortenedPowerSpectrum, 0, MAX_FREQ); double maxSignal = shortenedPowerSpectrum.Max(); shortenedPowerSpectrum = shortenedPowerSpectrum.Select(x => x / maxSignal).ToArray(); Array.Sort(shortenedPowerSpectrum, frequencyRange); Array.ConstrainedCopy(shortenedPowerSpectrum, 0, highestPowers, 0, NUM_MAX_FREQS); Array.ConstrainedCopy(frequencyRange, 0, highestFrequencies, 0, NUM_MAX_FREQS); for (int i = 0; i < NUM_MAX_FREQS; i++) { double[] sineWave = Generate.Sinusoidal(RECORDING_RATE * SAMPLE_DURATION, RECORDING_RATE, highestFrequencies [i], highestPowers [i]); outputData = outputData.Zip(sineWave, (x, y) => x + y).ToArray(); } float[] audioData = outputData.Select <double, float> (x => (float)x).ToArray(); return(audioData); }
private void FourierRecord_Click(object sender, EventArgs e) { if (!File.Exists("./Sounds/temp.wav")) { return; } ComplexSignal sComplex = FFTComplex("temp"); Complex[] channel = sComplex.GetChannel(0); chart2.Series.Clear(); chart2.Series.Add("FFT"); chart2.Series["FFT"].ChartType = SeriesChartType.FastLine; chart2.Series["FFT"].ChartArea = "ChartArea1"; double[] power = FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(channel)); double[] freqv = FilterVoiceHz(Accord.Audio.Tools.GetFrequencyVector(sComplex.Length, sComplex.SampleRate)); for (int i = 1; i < power.Length; i++) { chart2.Series["FFT"].Points.AddXY(freqv[i], power[i]); } }
void ProcessBaselinePowerSpectrum() { ComplexSignal cs = Signal.FromArray(baselineArray, SAMPLE_RATE).ToComplex(); cs.ForwardFourierTransform(); baselinePowerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0)); CubeStateManager.baselineEEG[(int)channelToRecordFrom] = baselineArray.Average(); }
private static double FindFrequencyCount(int sampleRate, Signal signal, double frequency) { ComplexSignal c = signal.ToComplex(); c.ForwardFourierTransform(); double[] freq = Accord.Audio.Tools.GetFrequencyVector(c.Length, sampleRate); double[] ms = Accord.Audio.Tools.GetMagnitudeSpectrum(c.GetChannel(0)); int idx = freq.Subtract(frequency).Abs().ArgMin(); return(ms[idx]); }
/// <summary> /// Computes (populates) a Spectrogram mapping with values from a signal. /// </summary> /// /// <param name="signal">The signal to be windowed in the spectrogram.</param> /// public void Compute(Signal signal) { this.frequencies = FourierTransform2.GetFrequencyVector(windowSize, signal.SampleRate); Signal[] signals = signal.Split(window: window, stepSize: windowSize); this.magnitudes = new double[signals.Length][]; for (int i = 0; i < signals.Length; i++) { ComplexSignal c = signals[i].ToComplex(); c.ForwardFourierTransform(); this.magnitudes[i] = FourierTransform2.GetMagnitudeSpectrum(c.GetChannel(channel)); } }
private void EqualsButton_Click(object sender, EventArgs e) { Dictionary <double, string> icorrelation = new Dictionary <double, string>(); if (!Directory.Exists("./Sounds")) { return; } string[] dir_Sounds = Directory.GetFiles("./Sounds"); double max = 0; foreach (var sound in dir_Sounds) { if (!(sound.Contains("temp.wav")) && (sound.Contains(".wav"))) { string name = sound.Replace("./Sounds", "").Replace(".wav", "").Replace(((char)92).ToString(), ""); ComplexSignal recordSignal = FFTComplex("temp"); Complex[] recordChannel = recordSignal.GetChannel(0); ComplexSignal bd = FFTComplex(name); Complex[] bdchannel = bd.GetChannel(0); double[] rpower = NormalizeStretch(FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(recordChannel)), 0, 1); double[] bdpower = NormalizeStretch(FilterVoiceHz(Accord.Audio.Tools.GetMagnitudeSpectrum(bdchannel)), 0, 1); double corr = ComputeCoeff(bdpower, rpower); Console.WriteLine(name + ":" + corr); icorrelation.Add(corr, name); if (corr >= max) { max = corr; } } } foreach (KeyValuePair <double, string> pair in icorrelation) { if (pair.Key == max) { MessageBox.Show(pair.Value); } } }
int status = 0; ///记录上次情绪状态 //Signal S2;用作第二通道 //ComplexSignal signal; private void fft_process() { S1 = Signal.FromArray(channel1, 250, SampleFormat.Format32BitIeeeFloat); //从浮点数组创建一个新的信号 signal = S1.ToComplex(); //将此信号转换为ComplexSignal信号 signal.ForwardFourierTransform(); //对ComplexSignal信号应用前向快速傅立叶变换 // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. Complex[] channel = signal.GetChannel(0); //从信号中提取通道 double[] power = Accord.Audio.Tools.GetPowerSpectrum(channel); //计算复杂信号的功率谱 double[] freqv = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate); //创建均匀间隔的频率向量(假设对称FFT) power[0] = 0; // zero DC float[] g = new float[power.Length]; for (int i = 0; i < power.Length; i++) { g[i] = (float)power[i]; signal1[i] = g[i] * 1000; SetpowerCC(g[i]); //输出power的值 SetFrequCC(freqv[i]); //输出freqv的值 } powerCaculate(); //对小于20hz的部分求和 pictureBox2.Refresh(); //重新绘制Channel1频域图 }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void ProcessFrame(float[,] channels, string stamp) { // We can start by converting the audio frame to a complex signal //Signal realSignal = Signal.FromArray(channels,WindowSize,8, 50, SampleFormat.Format32BitIeeeFloat); //ComplexSignal signal = ComplexSignal.FromSignal(realSignal); ComplexSignal signal = ComplexSignal.FromArray(channels, 50); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate); double[][] power = new double[signal.Channels][]; //Complex[] channel = signal.GetChannel(0); //double[] g = Tools.GetPowerSpectrum(channel); int[][] peaksIndex1 = new int[signal.Channels][]; int[][] peaksIndex2 = new int[signal.Channels][]; int SearchLength = 7; string content = stamp + ","; for (int i = 0; i < signal.Channels; i++) { //complexChannels[i] = signal.GetChannel(i); power[i] = Tools.GetPowerSpectrum(signal.GetChannel(i)); // zero DC power[i][0] = 0; double max = power[i].Max(); int position = Array.IndexOf(power[i], max); //normalize amplitude for (int n = 0; n < power[i].Length; n++) { power[i][n] = power[i][n] / max; } if (vibrateChannels.ContainsKey(i + 1)) { VibrateChannel vc = vibrateChannels[i + 1]; float[] floatList = power[i].Select(x => (float)x).ToArray(); AccWave awObject = new AccWave(vc.SensorId, "028", floatList); //byte[] result = serializer.PackSingleObject(awObject); //AppendLog(this.ip + " Frame Length: " + result.Length.ToString()); //udpClient.Send(result, result.Length, remoteEndPoint); //udpClient.Close(); } //if (!isCalculateCableForce) //{ // continue; //} double maxFrequency = freqv[position]; peaksIndex1[i] = power[i].FindPeaks(); if (peaksIndex1[i].Length < SearchLength) { continue; } double[] peaks2 = new double[peaksIndex1[i].Length]; for (int j = 0; j < peaksIndex1[i].Length; j++) { peaks2[j] = power[i][peaksIndex1[i][j]]; //low pass //if (freqv[peaksIndex1[i][j]] > 10) //{ // peaks2[j] = 0; //} } peaksIndex2[i] = MaxSort(SearchLength, peaks2); Array.Sort(peaksIndex2[i]); } udpClient.Close(); if (isUpdateChart) { if (chart1.InvokeRequired) { chart1.BeginInvoke(new MethodInvoker(() => { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < freqv.Length; i++) { chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); } if (isCalculateCableForce) { for (int k = 0; k < peaksIndex2[j].Length; k++) { chart1.Series[j + 16].Points[peaksIndex1[j][peaksIndex2[j][k]]].Label = freqv[peaksIndex1[j][peaksIndex2[j][k]]].ToString(); } } } chart1.Invalidate(); })); } else { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < freqv.Length; i++) { chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); } } chart1.Invalidate(); } } }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void ProcessSingleFrame(float[,] channels) { //float[] data = new float[WindowSize]; //// We can start by converting the audio frame to a complex signal //for(int i = 0; i < WindowSize; i++) //{ // data[i] = channels[1, i]; //} //Signal realSignal = Signal.FromArray(data, 50, SampleFormat.Format32BitIeeeFloat); //ComplexSignal signal = ComplexSignal.FromSignal(realSignal); ComplexSignal signal = ComplexSignal.FromArray(channels, 50); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate); //double[][] power = new double[8][]; Complex[] channel0 = signal.GetChannel(0); Complex[] channel1 = signal.GetChannel(1); Complex[] channel2 = signal.GetChannel(2); Complex[] channel3 = signal.GetChannel(3); double[] g0 = Tools.GetPowerSpectrum(channel0); double[] g1 = Tools.GetPowerSpectrum(channel1); double[] g2 = Tools.GetPowerSpectrum(channel2); double[] g3 = Tools.GetPowerSpectrum(channel3); g0[0] = 0; g1[0] = 0; g2[0] = 0; g3[0] = 0; //for(int i = 0; i < 8; i++) //{ // Complex[] channel = signal.GetChannel(i); // power[i] = Tools.GetPowerSpectrum(channel); // // zero DC // power[i][0] = 0; //} if (chart1.InvokeRequired) { chart1.BeginInvoke(new MethodInvoker(() => { chart1.Series[4].Points.Clear(); chart1.Series[5].Points.Clear(); chart1.Series[6].Points.Clear(); chart1.Series[7].Points.Clear(); for (int i = 0; i < g0.Length; i++) { chart1.Series[4].Points.AddXY(freqv[i], g0[i]); chart1.Series[5].Points.AddXY(freqv[i], g1[i]); chart1.Series[6].Points.AddXY(freqv[i], g2[i]); chart1.Series[7].Points.AddXY(freqv[i], g3[i]); } chart1.Invalidate(); })); } else { chart1.Series[4].Points.Clear(); for (int i = 0; i < g0.Length; i++) { chart1.Series[4].Points.AddXY(freqv[i], g0[i]); } chart1.Invalidate(); } }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void ProcessFrame(float[,] channels) { // We can start by converting the audio frame to a complex signal //Signal realSignal = Signal.FromArray(channels,WindowSize,8, 50, SampleFormat.Format32BitIeeeFloat); //ComplexSignal signal = ComplexSignal.FromSignal(realSignal); ComplexSignal signal = ComplexSignal.FromArray(channels, 50); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate); double[][] power = new double[8][]; Complex[][] complexChannels = new Complex[8][]; //Complex[] channel = signal.GetChannel(0); //double[] g = Tools.GetPowerSpectrum(channel); int[][] peaksIndex1 = new int[signal.Channels][]; int[][] peaksIndex2 = new int[signal.Channels][]; for (int i = 0; i < 8; i++) { //complexChannels[i] = signal.GetChannel(i); power[i] = Tools.GetPowerSpectrum(signal.GetChannel(i)); // zero DC power[i][0] = 0; peaksIndex1[i] = power[i].FindPeaks(); double[] peaks2 = new double[peaksIndex1[i].Length]; for (int j = 0; j < peaksIndex1[i].Length; j++) { peaks2[j] = power[i][peaksIndex1[i][j]]; } int[] index = peaks2.FindPeaks(); int[] rawIndex = new int[index.Length]; for (int k = 0; k < index.Length; k++) { rawIndex[k] = peaksIndex1[i][index[k]]; } peaksIndex2[i] = rawIndex; } //for (int j = 0; j < 8; j++) //{ // chart1.Series[j + 16].Points.Clear(); // for (int i = 0; i < freqv.Length; i++) // { // chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); // } //} for (int j = 0; j < 8; j++) { chart1.Series[j].Points.Clear(); for (int i = 0; i < peaksIndex1[j].Length; i++) { int frequencyIndex = peaksIndex1[j][i]; int powerIndex = peaksIndex1[j][i]; chart1.Series[j].Points.AddXY(freqv[frequencyIndex], power[j][powerIndex]); } chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < peaksIndex2[j].Length; i++) { int frequencyIndex = peaksIndex2[j][i]; int powerIndex = peaksIndex2[j][i]; chart1.Series[j + 16].Points.AddXY(freqv[frequencyIndex], power[j][powerIndex]); } } chart1.Invalidate(); }
private static WavechartBox show(String title, bool hold, params Tuple <Signal, int>[] series) { WavechartBox form = null; Thread formThread = null; AutoResetEvent stopWaitHandle = new AutoResetEvent(false); formThread = new Thread(() => { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Show control in a form form = new WavechartBox(); form.Text = title; form.formThread = formThread; if (!String.IsNullOrEmpty(title)) { form.zedGraphControl.GraphPane.Title.IsVisible = true; form.zedGraphControl.GraphPane.Title.Text = title; } var sequence = new ColorSequenceCollection(series.Length); for (int i = 0; i < series.Length; i++) { var signal = series[i].Item1; int channel = series[i].Item2; ComplexSignal complex = signal as ComplexSignal; if (complex != null && complex.Status != ComplexSignalStatus.Normal) { double[] spectrum = Accord.Audio.Tools.GetPowerSpectrum(complex.GetChannel(channel)); double[] frequencies = Accord.Audio.Tools.GetFrequencyVector(signal.Length, signal.SampleRate); form.series.Add(new LineItem(i.ToString(), frequencies, spectrum, sequence.GetColor(i), SymbolType.None)); form.zedGraphControl.GraphPane.XAxis.Title.Text = "Frequency"; form.zedGraphControl.GraphPane.YAxis.Title.Text = "Power"; } else { double[] values; if (signal.Channels == 1) { values = signal.ToDouble(); } else { ExtractChannel extract = new ExtractChannel(channel); values = extract.Apply(signal).ToDouble(); } form.series.Add(new LineItem(i.ToString(), Matrix.Indices(0, signal.Length).ToDouble(), values, sequence.GetColor(i), SymbolType.None)); form.zedGraphControl.GraphPane.XAxis.Title.Text = "Time"; form.zedGraphControl.GraphPane.YAxis.Title.Text = "Amplitude"; } } form.zedGraphControl.GraphPane.AxisChange(); stopWaitHandle.Set(); Application.Run(form); }); formThread.SetApartmentState(ApartmentState.STA); formThread.Start(); stopWaitHandle.WaitOne(); if (!hold) { formThread.Join(); } return(form); }
void ProcessData() { double[] powerSpectrum; ComplexSignal cs = Signal.FromArray(dataBuffer, SAMPLE_RATE).ToComplex(); cs.ForwardFourierTransform(); powerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0)); //Convert to dB powerSpectrum = powerSpectrum.Select((double arg, int index) => (10 * Math.Log10(arg / baselinePowerSpectrum[index]))).ToArray(); // Normalize values double minPowerSpectrum = (powerSpectrum.Min()); double maxPowerSpectrum = (powerSpectrum.Max()); powerSpectrum = powerSpectrum.Select((double arg, int index) => (arg - minPowerSpectrum) / (maxPowerSpectrum - minPowerSpectrum)).ToArray(); //Pick the color scheme according to the peak frequency - Blue-Green if peak below alpha, //Yellow-Red otherwise int highestFrequency = Array.IndexOf(powerSpectrum, powerSpectrum.Max()); currentScheme = highestFrequency >= alphaCutoff ? (highestFrequency >= gammaCutoff ? COLOUR_SCHEME.YELLOW_RED : COLOUR_SCHEME.PURPLE_RED) : COLOUR_SCHEME.BLUE_GREEN; Color[] spectrogramColours; if (currentScheme == COLOUR_SCHEME.BLUE_GREEN) { spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2BlueGreen(arg, 0.75, 0.5))).ToArray(); } else if (currentScheme == COLOUR_SCHEME.PURPLE_RED) { spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2PurpleRed(arg, 0.75, 0.5))).ToArray(); } else { spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2YellowRed(arg, 0.75, 0.5))).ToArray(); } //Set the texture for (int i = 0; i < numFreqs; i++) { texture.SetPixel(i, (currentWindow % maxTextureWidth), spectrogramColours[i]); } currentWindow++; texture.Apply(); //Set the normal map float xLeft; float xRight; float yUp; float yDown; float yDelta; float xDelta; for (int i = 0; i < numFreqs; i++) { int y = (currentWindow % maxTextureWidth); yUp = normalMap.GetPixel(y - 1, i).grayscale; yDown = normalMap.GetPixel(y + 1, i).grayscale; int tempI = (int)Mathf.Clamp(i, 1, numFreqs - 2); xLeft = (float)powerSpectrum[tempI - 1]; xRight = (float)powerSpectrum[tempI + 1]; xDelta = ((xLeft - xRight) + 1) * 0.5f; yDelta = ((yUp - yDown) + 1) * 0.5f; normalMap.SetPixel(i, y, new Color(xDelta, yDelta, 1.0f, yDelta)); } normalMap.Apply(); }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void ProcessFrame(float[,] channels, string stamp) { // We can start by converting the audio frame to a complex signal //Signal realSignal = Signal.FromArray(channels,WindowSize,8, 50, SampleFormat.Format32BitIeeeFloat); //ComplexSignal signal = ComplexSignal.FromSignal(realSignal); ComplexSignal signal = ComplexSignal.FromArray(channels, 50); //AppendLog(this.ip + "ProcessFrame Start "); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate); double[][] power = new double[signal.Channels][]; //Complex[] channel = signal.GetChannel(0); //double[] g = Tools.GetPowerSpectrum(channel); int[][] peaksIndex1 = new int[signal.Channels][]; int[][] peaksIndex2 = new int[signal.Channels][]; int SearchLength = 7; string content = stamp + ","; //IPAddress remoteIp = IPAddress.Parse(this.spectrumIP); ////int port = int.Parse(this.spectrumPort); //IPEndPoint remoteEndPoint = new IPEndPoint(remoteIp, this.spectrumPort); //UdpClient udpClient = new UdpClient(); //Dictionary<RedisKey, RedisValue> pair = new Dictionary<RedisKey, RedisValue>(); //IDatabase db_result = this.redis.GetDatabase(5); for (int i = 0; i < signal.Channels; i++) { //AppendLog(this.ip + "ProcessFrame calculate frame"); //complexChannels[i] = signal.GetChannel(i); power[i] = Tools.GetPowerSpectrum(signal.GetChannel(i)); // zero DC power[i][0] = 0; //test log //for (int n = 0; n < power[i].Length; n++) //{ // power[i][n] = Math.Log10(power[i][n]+0.0000000001); //} double max = power[i].Max(); int position = Array.IndexOf(power[i], max); //normalize amplitude for (int n = 0; n < power[i].Length; n++) { power[i][n] = power[i][n] / max; } if (vibrateChannels.ContainsKey(i + 1)) { //AppendLog(this.ip + "ProcessFrame Send Spectrum"); VibrateChannel vc = vibrateChannels[i + 1]; float[] floatList = power[i].Select(x => (float)x).ToArray(); //AccWave awObject = new AccWave(vc.SensorId, "028", floatList); //byte[] result = serializer.PackSingleObject(awObject); //AppendLog(this.ip + " Frame Length: " + result.Length.ToString()); //udpClient.Send(result, result.Length, remoteEndPoint); //udpClient.Close(); } //if (!isCalculateCableForce) //{ // continue; //} double maxFrequency = freqv[position]; peaksIndex1[i] = power[i].FindPeaks(); if (peaksIndex1[i].Length < SearchLength) { continue; } double[] peaks2 = new double[peaksIndex1[i].Length]; for (int j = 0; j < peaksIndex1[i].Length; j++) { peaks2[j] = power[i][peaksIndex1[i][j]]; //low pass //if (freqv[peaksIndex1[i][j]] > 10) //{ // peaks2[j] = 0; //} } peaksIndex2[i] = MaxSort(SearchLength, peaks2); Array.Sort(peaksIndex2[i]); double[] frequencies = new double[SearchLength]; try { for (int k = 0; k < SearchLength; k++) { frequencies[k] = freqv[peaksIndex1[i][peaksIndex2[i][k]]]; } } catch (Exception ex) { } double frequency = 0; double frequency1 = FindFFWithFundamentalFreqency(frequencies, maxFrequency); List <double> candidateFrequencies = SearchCandidateFrequencyWithSpacing(frequencies, frequencies.Length, 0.3); double frequency2 = FindFFWithSpacing(frequency1, candidateFrequencies.ToArray()); if (Math.Abs(frequency1 - frequency2) < 0.15) { frequency = (frequency2 + frequency1) / 2; } content += frequency.ToString(); content += ","; //AppendLog("Channel " + (i + 1).ToString() + " "); //if ((i + 1) == (int)numericUpDownChannel.Value) //{ // //AppendLog("Channel " + (i + 1).ToString() + " f1: " + frequency1.ToString() + " f2: " + frequency2.ToString() + " Fundamental frequency:" + frequency.ToString()); // if (frequency != 0) // { // //string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // string str = ""; // str += stamp + " "; // if (redis.IsConnected) // { // } // else // { // str += "redis server is not connected"; // //lthis.AppendLog(str); // return; // } // if (vibrateChannels.ContainsKey(i + 1)) // { // double force = 0; // VibrateChannel vc = vibrateChannels[i+1]; // force = Math.Round(CalculateCableForce(vc, frequency) / 1000); // string key = vc.SensorId + "-012"; // DataValue dv = new DataValue(); // dv.SensorId = vc.SensorId; // dv.TimeStamp = stamp; // dv.ValueType = "012"; // dv.Value = frequency; // //resultQueue.Enqueue(dv); // string result = JsonConvert.SerializeObject(dv); // pair[key] = result; // if (force != 0) // { // key = vc.SensorId + "-013"; // DataValue dv1 = new DataValue(); // dv1.SensorId = vc.SensorId; // dv1.TimeStamp = stamp; // dv1.ValueType = "013"; // dv1.Value = force; // string result1 = JsonConvert.SerializeObject(dv1); // pair[key] = result; // //resultQueue.Enqueue(dv1); // } // AppendLog("Channel " + (i + 1).ToString() + " frequency: " + frequency.ToString() + " Cable Force: " + force.ToString()); // } // } //} } //if (pair.Count > 0) //{ // db_result.StringSet(pair.ToArray()); // pair.Clear(); //} //content = content.Remove(content.Length - 1); //udpClient.Close(); //AppendResult(content); //return; if (isUpdateChart) { if (chart1.InvokeRequired) { chart1.BeginInvoke(new MethodInvoker(() => { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < freqv.Length; i++) { chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); } if (peaksIndex2[j] == null) { continue; } for (int k = 0; k < peaksIndex2[j].Length; k++) { chart1.Series[j + 16].Points[peaksIndex1[j][peaksIndex2[j][k]]].Label = freqv[peaksIndex1[j][peaksIndex2[j][k]]].ToString(); } } chart1.Invalidate(); })); } else { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < freqv.Length; i++) { chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); } } chart1.Invalidate(); } } //AppendLog(this.ip+" ProcessFrame Finish "); }
public void frequencyData(ComplexSignal curSig, out double[] powers, out double[] magnitudes, out double[] freq) { powers = Accord.Audio.Tools.GetPowerSpectrum(curSig.GetChannel(0)); magnitudes = Accord.Audio.Tools.GetMagnitudeSpectrum(curSig.GetChannel(0)); freq = Accord.Audio.Tools.GetFrequencyVector(curSig.Length, curSig.SampleRate); }
/// <summary> /// This method will be called whenever there is a new audio /// frame to be processed. /// </summary> /// void ProcessFrame(float[,] channels) { // We can start by converting the audio frame to a complex signal //Signal realSignal = Signal.FromArray(channels,WindowSize,8, 50, SampleFormat.Format32BitIeeeFloat); //ComplexSignal signal = ComplexSignal.FromSignal(realSignal); ComplexSignal signal = ComplexSignal.FromArray(channels, 50); // If its needed, if (window != null) { // Apply the chosen audio window signal = window.Apply(signal, 0); } // Transform to the complex domain signal.ForwardFourierTransform(); // Now we can get the power spectrum output and its // related frequency vector to plot our spectrometer. double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate); double[][] power = new double[signal.Channels][]; FrequencyPoint[][] fps = new FrequencyPoint[8][]; int[][] peaksIndex1 = new int[signal.Channels][]; int[][] peaksIndex2 = new int[signal.Channels][]; for (int i = 0; i < signal.Channels; i++) { //complexChannels[i] = signal.GetChannel(i); power[i] = Tools.GetPowerSpectrum(signal.GetChannel(i)); // zero DC power[i][0] = 0; //fps[i] = GetFrequencyPoints(power[i],freqv); peaksIndex1[i] = power[i].FindPeaks(); double[] peaks2 = new double[peaksIndex1[i].Length]; for (int j = 0; j < peaksIndex1[i].Length; j++) { peaks2[j] = power[i][peaksIndex1[i][j]]; } int[] index = peaks2.FindPeaks(); int[] rawIndex = new int[index.Length]; for (int k = 0; k < index.Length; k++) { rawIndex[k] = peaksIndex1[i][index[k]]; } peaksIndex2[i] = rawIndex; } //int[] peaksIndex = power[0].FindPeaks(); //string content=""; //foreach(int index in peaksIndex) //{ // content += (freqv[index] + " "); //} //content += "\r\n"; //AppendLog(content); if (isUpdateChart) { if (chart1.InvokeRequired) { chart1.BeginInvoke(new MethodInvoker(() => { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); //for (int i = 0; i < freqv.Length; i++) for (int i = 0; i < peaksIndex2[j].Length; i++) { //chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); chart1.Series[j + 16].Points.AddXY(freqv[peaksIndex2[j][i]], power[j][peaksIndex2[j][i]]); //chart1.Series[j + 16].Points.AddXY(freqv[peaksIndex1[j][i]], power[j][peaksIndex1[j][i]]); //chart1.Series[j + 16].Points[i].ToolTip = freqv[i].ToString(); } } chart1.Invalidate(); })); } else { for (int j = 0; j < signal.Channels; j++) { chart1.Series[j + 16].Points.Clear(); for (int i = 0; i < freqv.Length; i++) { chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]); chart1.Series[j + 16].Points[i].ToolTip = freqv[i].ToString(); } } chart1.Invalidate(); } } //保存频谱 /* * StringBuilder sb = new StringBuilder(2048); * string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); * //sb.AppendLine(stamp + ","); * for(int i=0;i< signal.Channels; i++) * { * if (vibrateChannels.ContainsKey(i + 1)) * { * sb.Append(vibrateChannels[i + 1] + "," + stamp + ","); * * for (int j = 0; j < freqv.Length; j++) * { * sb.Append(power[i][j] + ","); * } * sb.Remove(sb.Length - 1, 1); * sb.Append("\r\n"); * } * } * * //AppendLog(sb.ToString()); * AppendRecord(sb); */ }