コード例 #1
0
        private double CalculateCableForce(VibrateChannel channel, double frequency)
        {
            double result = 0;

            result = 4 * channel.Mass * channel.Length * channel.Length * frequency * frequency;
            return(result);
        }
コード例 #2
0
 private void LoadChannels()
 {
     using (SQLiteConnection connection = new SQLiteConnection(this.database))
     {
         connection.Open();
         string        strainStatement = "select SensorId,ChannelNo from Channels where GroupNo ='" + this.deviceId + "'";
         SQLiteCommand command         = new SQLiteCommand(strainStatement, connection);
         using (SQLiteDataReader reader = command.ExecuteReader())
         {
             while (reader.Read())
             {
                 //string  groupId = reader.GetString(0);
                 string         sensorId  = reader.GetString(0);
                 int            channelNo = reader.GetInt32(1);
                 VibrateChannel vc        = new VibrateChannel(sensorId, channelNo, 0, 0);
                 vibrateChannels.Add(channelNo, vc);
             }
         }
     }
 }
コード例 #3
0
        /// <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();
                }
            }
        }
コード例 #4
0
        /// <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 ");
        }