This page is under reconstruction...

Freqency tracking app

FRF Analytic function
        using System;
        using System.Numerics;
        
        
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        
        // important stuff
        using System.Windows.Forms.DataVisualization.Charting;
        
        // GENERATOR 
        
        using MathNet.Numerics;                   
        using MathNet.Numerics.IntegralTransforms;
        
        
        
        
        namespace CHRreader_2
        {
               
            public partial class Form2 : Form
            {
        
                #region pointer functinos/ DELEGATES
                private delegate void ptrf_SpectrumLineChanged();
                private event ptrf_SpectrumLineChanged RadButtonPressed;
                #endregion
        
                #region FORM3 interactor globals      
        
                Form3 freq_tecker;
                public event ptrf_startPressed genRuns;      // evet which goes to form 3 
        
                public static bool spectRuns = false;        // flag,when spectralizer runs 
                public static List<double> magFFT, fftBins;  // export lists for conroler form 3. 
        
                #endregion 
        
                #region GLOBALS
        
                double fs; // take from function generator 
               
             
                #endregion
        
                #region SPECTRAL- TIMER
        
                private Timer spectWatch = new Timer();
                static int counter = 0;
                int tInterval;
        
                private void InitializeSpectTimer()
                {
        
                    tInterval = Convert.ToInt32(comboBox1.SelectedItem);
                    if (tInterval == 0)
                        tInterval = 500;
                    
               
                    spectWatch.Interval = tInterval;
                    spectWatch.Tick += new EventHandler(RefreshSpectra);   // what happens after tick
                    spectWatch.Tick += new EventHandler(ReceiveTicks);     // Received within the class
                    spectWatch.Tick += new EventHandler(EmitTicks);        // emitted outside of class
                    spectWatch.Start();
                    spectRuns = true;
                }
                //
                //------| ON EVERY TICK FOLLOWING FUCNTIONS ARE CALLED       
                        void RefreshSpectra(object sender, EventArgs e)
                        {
                            counter++;  // count only how many FFTs ticked
                                        // by the spectral watch                                               
        
                
                        chart1.Series["Series1"].Points.Clear();
                 
                         if (Form1.runTime == true)
                            {      
                                     
        
                                lblGen.Text = "Gen. runs";
                                panelGen.BackColor = Color.LightGreen;
                              
                                computeFFT();
                                
                            }
                            else
                            {                            
                                    spectWatch.Stop();
                                    lblStatus.Text = "Loop stoped";
                                    panelGen.BackColor = Color.Gray;
                                    lblGen.Text = "Gen. stopped";
                                    spectRuns = false;
                            }
                        }
                
                        void ReceiveTicks(object sender, EventArgs e)
                        {
        
                            if (Form1.runTime == true)
                            {
                                progressBar1.Increment(1);
        
                                if (progressBar1.Value == progressBar1.Maximum)
                                {
                                    progressBar1.Value = 1;
                                }
                            }
        
                             lblStatus.Text = "Freq.Bins: " + magFFT.Count.ToString();
        
                        }
                                
                        void EmitTicks(object sender, EventArgs e)
                        {
                            if (freq_tecker != null)
                            {
                                    freq_tecker.setUI(); //
                                    freq_tecker.setSpectFlag();
                            }                  
                        }
                #endregion
        
                #region CONSTRUCTOR
                public Form2()
                {
        
                ///
                /// Class instance 
                ///
                    InitializeComponent();
                    StartPosition = FormStartPosition.Manual;
                    Location = new Point(910, 50);
           
                    fs = Form1.sampFreq;
                    cmdLoop.Visible = false;
                    panelLoop.Visible = false;
                    magFFT = new List();
                    fftBins = new List();
        
                    RadButtonPressed += new ptrf_SpectrumLineChanged(LineChecker);
                     chart1.Series["Series1"].Points.AddXY(0, 0);
        
                    //chart1.Series["Series1"].Points.AddXY(0, 1);
                    //chart1.Series["Series1"].Points.AddXY(1, 1);
                    //chart1.Series["Series1"].Points.AddXY(1, 10);
        
        
        
                }
                #endregion
        
                #region UI BASED EVENTS 
        
                void LineChecker()
                {
        
                    // This function is called exclusively by the event 
                    // RedbuttonPressed()
                    //
                    //
        
                    if (radBins.Checked)
                    {
                        chart1.Series["Series1"].ChartType = SeriesChartType.Column;
                    }
                    else if (radLine.Checked)
                    {
                        chart1.Series["Series1"].ChartType = SeriesChartType.Line;
        
                    }
                    else if (radAuto.Checked)
                    {                
                       
                        if (magFFT.Count < 128)
                        {
                            chart1.Series["Series1"].ChartType = SeriesChartType.Column;
                        }
                        else if (magFFT.Count > 128)
                        {
                            chart1.Series["Series1"].ChartType = SeriesChartType.Line;
                        }
        
                    }         
        
        
                }
        
                // event of changing rad button checked calls another event that triggeres
                // function: LineChecker
                private void radBins_CheckedChanged(object sender, EventArgs e) { RadButtonPressed(); }
                private void radLine_CheckedChanged(object sender, EventArgs e) { RadButtonPressed(); }
                private void radAuto_CheckedChanged(object sender, EventArgs e) { RadButtonPressed(); }
        
        
        
                // when stop button clicked 
                private void button1_Click(object sender, EventArgs e)
                {
                 
                    spectWatch.Stop();
                    spectRuns = false;
        
                    if (freq_tecker != null)
                    {
                        freq_tecker.setUI();
                        freq_tecker.setSpectFlag();
                    }
        
                   
                    progressBar1.Value = 100;
                    counter = 0;
                    lblStatus.Text = "Loop stoped";
                    
                }
        
                // radio buttons for enabling loop 
                private void radLoop_CheckedChanged(object sender, EventArgs e)
                {
        
                    if (radLoop.Checked)
                    {
                        cmdLoop.Visible = true;
                        panelLoop.Visible = true; 
                        
                    }
        
                    if (!radLoop.Checked)
                    {
                        cmdLoop.Visible = false;
                        panelLoop.Visible = false;
                    }
        
                }
        
        
                // cmdLoop 
                private void cmdLoop_Click(object sender, EventArgs e)
                {
                    counter = 0;
                    InitializeSpectTimer();
        
                }
        
                // compute FFT after button clicked... 
                private void cmdCompute_Click(object sender, EventArgs e)
                {
                    lblStatus.Text = "FFT...";
                    chart1.Series["Series1"].Points.Clear();
                    computeFFT();
        
                    if (Form1.runTime == false)
                    {
                        panelGen.BackColor = Color.Gray;
                        lblGen.Text = "Gen. stopped";
                    }
                    else
                    {
                        panelGen.BackColor = Color.LightGreen;
                        lblGen.Text = "Gen. runs";
        
                    }      
        
        
                }
        
        
                // zooming the fft .. 
                private void trackBar1_Scroll(object sender, EventArgs e)
                {
                               
                    chart1.Series["Series1"].Points.Clear();
                    computeFFT();
        
                }
                // close the form ... 
                private void cmdClose_Click(object sender, EventArgs e)
                {
                    spectWatch.Stop();
                    Close();
                }
                #endregion
        
                #region AUX/ FREQUENCY ANALYSIS
        
                void computeFFT()
                {
                     This content is :
                    ____  ____   ___   ______    ___    __ ______    ___  ___   
                    |    \|    \ /   \ |      |  /  _]  /  ]      |  /  _]|   \  
                    |  o  )  D  )     ||      | /  [_  /  /|      | /  [_ |    \ 
                    |   _/|    /|  O  ||_|  |_||    _]/  / |_|  |_||    _]|  D  |
                    |  |  |    \|     |  |  |  |   [_/   \_  |  |  |   [_ |     |
                    |  |  |  .  \     |  |  |  |     \     | |  |  |     ||     |
                    |__|  |__|\_|\___/   |__|  |_____|\____| |__|  |_____||_____|
                                                                                 
                    Contact me for details..
                            
                }
        
        
        
        
                #endregion
        
                #region FORM3/ output functions and callers 
        
                // start the FREQUENCY CONTROL 
                private void cmdTrack_Click(object sender, EventArgs e)
                {
                    
        
                    if (freq_tecker == null)
                    {
                        freq_tecker = new Form3();
                        freq_tecker.Show();
                    }
                    else if (freq_tecker != null)
                    {
                        freq_tecker.Close();
                        freq_tecker = new Form3();
                        freq_tecker.Show(); 
                    
                    }
                }
        
                private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    tInterval = Convert.ToInt32(comboBox1.SelectedItem);
                    Console.WriteLine("time interval {0}", tInterval);
                    spectWatch.Interval = tInterval;
                }
        
                #endregion
        
                #region FORM1/input events 
        
        
                public void setSemaphore()
                {
                    if (Form1.runTime == true)
                    {
                        panelGen.BackColor = Color.LightGreen;
                    }
                    else if (Form1.runTime == false)
                    {
                        panelGen.BackColor = Color.DarkGray;
                    }
        
                    if (freq_tecker != null)
                    {
                        genRuns += new ptrf_startPressed(freq_tecker.setUI);
                        genRuns();
                        
                    }
        
                   
                }
        
                #endregion
            }
        }