//dotnet run public static void Main(string[] args) { Console.WriteLine("Tryout"); var brain = new IoUnit("10.77.0.10"); brain.SendPowerupClear(); brain.SetDigitalPointState(4 + 1, true); var counter = new FrequencyPoint(); while (true) { var abank = brain.ReadAnalogBank(); var dbank = brain.ReadDigitalBank(); var latches0 = brain.ReadModuleLatches(0); var loglatch = false; for (var i = 0; i < 8; i++) { if (latches0[i]) { loglatch = true; } } if (loglatch) { for (var i = 0; i < 8; i++) { Console.Write(latches0[i]? "1" : "0"); } Console.WriteLine(); } if (latches0[0]) { counter.AddTransition(); } if (latches0[4]) { counter.AddTransition(); } counter.UpdateFrequency(); /* * //Console.WriteLine(ToHex(dbank, 0, dbank.Length)); * //Console.WriteLine(ToHex(abank, 0, abank.Length)); * Console.WriteLine("F:{0} DM:[{1} {2} {3} {4}] AM:[{5:0.0} {6:0.0}]", * counter.Value, * brain.GetDigitalPointState(dbank, 0), * brain.GetDigitalPointState(dbank, 1), * brain.GetDigitalPointState(dbank, 2), * brain.GetDigitalPointState(dbank, 3), * brain.GetAnalogPointValue(abank, 16), * brain.GetAnalogPointValue(abank, 17)); */ } }
private FrequencyPoint[] GetFrequencyPoints(double[] power, double[] freqv) { FrequencyPoint[] fps = new FrequencyPoint[4]; double[] powerCpy = (double[])power.Clone(); double[] freqCpy = (double[])freqv.Clone(); Array.Sort(powerCpy, freqCpy); fps[0] = new FrequencyPoint(freqCpy[freqCpy.Length - 1], powerCpy[freqCpy.Length - 1]); fps[1] = new FrequencyPoint(freqCpy[freqCpy.Length - 2], powerCpy[freqCpy.Length - 2]); fps[2] = new FrequencyPoint(freqCpy[freqCpy.Length - 3], powerCpy[freqCpy.Length - 3]); fps[3] = new FrequencyPoint(freqCpy[freqCpy.Length - 4], powerCpy[freqCpy.Length - 4]); return(fps); }
/// <summary> /// RetrieveFrequency /// </summary> /// <param name="collectorId"></param> /// <returns></returns> public FrequencyPoint RetrieveFrequency(Guid collectorId) { var collector = context.Collectors.Where(@w => @w.Id == collectorId).First(); var frequency = context.CollectorFrequencies.Where(@w => @w.Id == collector.FrequencyId).First(); var slots = context.CollectorSlots.Where(@w => @w.FrequencyId == collector.FrequencyId).ToList(); FrequencyPoint frequencyPoint = new FrequencyPoint { PickupFrequency = frequency.PickupFrequency, FrequencyType = frequency.FrequencyType, Capacity = frequency.Capacity, LastUpdateDate = frequency.LastUpdateDate, }; if (slots.Count() == 1) { frequencyPoint.SlotFrom1 = slots.ElementAt(0).SlotFrom; frequencyPoint.SlotTo1 = slots.ElementAt(0).SlotTo; } else if (slots.Count() == 2) { frequencyPoint.SlotFrom1 = slots.ElementAt(0).SlotFrom; frequencyPoint.SlotTo1 = slots.ElementAt(0).SlotTo; frequencyPoint.SlotFrom2 = slots.ElementAt(1).SlotFrom; frequencyPoint.SlotTo2 = slots.ElementAt(1).SlotTo; } else if (slots.Count() == 3) { frequencyPoint.SlotFrom1 = slots.ElementAt(0).SlotFrom; frequencyPoint.SlotTo1 = slots.ElementAt(0).SlotTo; frequencyPoint.SlotFrom2 = slots.ElementAt(1).SlotFrom; frequencyPoint.SlotTo2 = slots.ElementAt(1).SlotTo; frequencyPoint.SlotFrom3 = slots.ElementAt(2).SlotFrom; frequencyPoint.SlotTo3 = slots.ElementAt(2).SlotTo; } return(frequencyPoint); }
/// <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); */ }