Ejemplo n.º 1
0
        private void DisplayColorMap(object sender, EventArgs e)
        {
            // Enter a null time data for showing the ticks and label of axis
            chartADC.Series["ADC0"].Points.Clear();
            chartADC.Series["ADC0"].Points.AddXY(0, 0);

            try
            {
                // Set a frequency and time width of the backimage of Chart background
                float dFrq = (float)(bMapHeight / ((NFFT / 2.0f) - 1.0f));

                // Get a new memory bitmap with the same size, as the picture box
                bMapCurrent = new Bitmap(chartADC.Images[0].Image);
                bMapNew     = new Bitmap(bMapWidth, bMapHeight);

                // Graphics a new graphics
                Graphics grphNew = Graphics.FromImage(bMapNew);

                // Shift Graphics by dxScroll
                grphNew.DrawImage(bMapCurrent, new PointF(-dxScroll, 0));

                // Convert FFT values into Byte range within Max. and Min.
                byte[] SPLband = new byte[(NFFT / 2)];
                for (int Idx = 0; Idx < (NFFT / 2); Idx++)
                {
                    // dBA calculation
                    float Af;
                    if (cbAweight.Checked == true)
                    {
                        Af = dBA_Weight[Idx];
                    }
                    else
                    {
                        Af = 0.0f;
                    }

                    double tmpSPL = 20 * Math.Log10(FFT0[Idx]) + Af;

                    if (tmpSPL > MaxValue)
                    {
                        tmpSPL = MaxValue;
                    }
                    else if (tmpSPL < MinValue)
                    {
                        tmpSPL = MinValue;
                    }

                    SPLband[(NFFT / 2) - (Idx + 1)] = (byte)(Math.Floor(255 / (MinValue - MaxValue) * (tmpSPL - MaxValue)));
                }

                // Copy the latest data into the most-right column
                for (int Idx = 0; Idx < (NFFT / 2) - 1; Idx++)
                {
                    // Create a Rectangle for each single frequency line
                    RectangleF RectFrq = new RectangleF(bMapWidth - dxScroll, Idx * dFrq, dxScroll, dFrq);

                    // Create a linear gradient brush for a signel Rectangle
                    LinearGradientBrush LinearBrush = new LinearGradientBrush(RectFrq,
                                                                              Color.FromArgb(SPLband[Idx], SPLband[Idx], SPLband[Idx]),
                                                                              Color.FromArgb(SPLband[Idx + 1], SPLband[Idx + 1], SPLband[Idx + 1]),
                                                                              LinearGradientMode.Vertical);

                    // Fill a color of Rectangle with the created linear gradient
                    grphNew.FillRectangle(LinearBrush, RectFrq);
                }

                // Print the bitmap with colorization
                bMapNew = ColorMapClass.Colorize(bMapNew, 255, JetColorMap);

                // Copy the new map into the backimage of Chart background
                if (chartADC.Images.Count != 0)
                {
                    chartADC.Images.RemoveAt(0);
                }

                // TO COMMENT
                chartADC.Images.Add(new NamedImage("ColorMapImage", bMapNew));
                chartADC.ChartAreas[0].BackImage         = chartADC.Images[0].Name;
                chartADC.ChartAreas[0].BackImageWrapMode = ChartImageWrapMode.Scaled;
            }

            catch
            {
            }
        }
Ejemplo n.º 2
0
        private void cbColormap_CheckedChanged(object sender, EventArgs e)
        {
            if (cbColormap.Checked != true)
            {
                // Activate and deactivate buttons and colormap scale
                lbl_Yint.Enabled        = true;
                tb_Yint.Enabled         = true;
                pbColorMapScale.Visible = false;

                // Initiailze a chart background
                chartADC.ChartAreas[0].BackImage = "";
                chartADC.ChartAreas[0].BackColor = Color.White;

                // Change a chart setting for FFT
                chartADC.Text = "FFT";
                chartADC.ChartAreas[0].AxisX.Title    = "Frequency [Hz]";
                chartADC.ChartAreas[0].AxisX.Minimum  = 0;
                chartADC.ChartAreas[0].AxisX.Maximum  = 2000;
                chartADC.ChartAreas[0].AxisX.Interval = 400;
                chartADC.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                chartADC.ChartAreas[0].AxisX.MajorGrid.Interval      = 200;
                if (cbAweight.Checked == true)
                {
                    chartADC.ChartAreas[0].AxisY.Title = "FFTvalue [dBA]";
                }
                else
                {
                    chartADC.ChartAreas[0].AxisY.Title = "FFTvalue [dB]";
                }
                chartADC.ChartAreas[0].AxisY.Minimum  = -60;
                chartADC.ChartAreas[0].AxisY.Maximum  = 0;
                chartADC.ChartAreas[0].AxisY.Interval = 10;
                chartADC.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                chartADC.ChartAreas[0].AxisY.MajorGrid.Interval      = 10;

                // Set a scale box
                tb_Ymax.Text = "0";
                tb_Ymin.Text = "-60";
                tb_Yint.Text = "10";

                // Set a max and mion value
                MaxValue = Convert.ToDouble(tb_Ymax.Text);
                MinValue = Convert.ToDouble(tb_Ymin.Text);
                chartADC.ChartAreas[0].AxisY.Crossing = MinValue;
            }
            else
            {
                // Activate and deactivate buttons
                lbl_Yint.Enabled = false;
                tb_Yint.Enabled  = false;

                // Initiailze a color map scale
                pbColorMapScale.Visible = true;

                // Create a new memory bitmap the same size as the picture box
                Bitmap mapColorScale = new Bitmap(pbColorMapScale.Width, pbColorMapScale.Height);

                // Create a graphsics object from our memory bitmap, so we can draw on it and clear
                Graphics Canvas = Graphics.FromImage(mapColorScale);

                // Create a rectangle area
                Rectangle RectColorScale = new Rectangle(0, 0, pbColorMapScale.Width, pbColorMapScale.Height);

                // Create a new color belnd to tell the LinearGradientBrush what colors to use and where to put them
                LinearGradientBrush LinearBrush = new LinearGradientBrush(RectColorScale, Color.Black, Color.White, LinearGradientMode.Vertical);

                // Pass off color blend to LinearGradientBrush to instruct it how to generate the gradient
                Canvas.FillRectangle(LinearBrush, RectColorScale);

                // Copy the Bitmap into the image of picture box
                pbColorMapScale.Image = ColorMapClass.Colorize(mapColorScale, 255, JetColorMap);

                // Get the pixel position of real plotting chart area
                double PixelXmin = chartADC.ChartAreas[0].AxisX.ValueToPixelPosition(chartADC.ChartAreas[0].AxisX.Minimum);
                double PixelXmax = chartADC.ChartAreas[0].AxisX.ValueToPixelPosition(chartADC.ChartAreas[0].AxisX.Maximum);
                double PixelYmin = chartADC.ChartAreas[0].AxisY.ValueToPixelPosition(chartADC.ChartAreas[0].AxisY.Minimum);
                double PixelYmax = chartADC.ChartAreas[0].AxisY.ValueToPixelPosition(chartADC.ChartAreas[0].AxisY.Maximum);

                // Get the size of bitmap in terms of pixel number
                bMapWidth  = (int)Math.Abs(PixelXmax - PixelXmin);
                bMapHeight = (int)Math.Abs(PixelYmax - PixelYmin);

                // Change a chart setting for FFT
                chartADC.Text = "Color map";
                chartADC.ChartAreas[0].AxisX.Title    = "Time [sec]";
                chartADC.ChartAreas[0].AxisX.Minimum  = 0;
                chartADC.ChartAreas[0].AxisX.Maximum  = (NFFT / Fs) * bMapWidth / dxScroll;
                chartADC.ChartAreas[0].AxisX.Interval = 1;
                chartADC.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                chartADC.ChartAreas[0].AxisX.MajorGrid.Interval      = 2;
                chartADC.ChartAreas[0].AxisY.Title    = "Frequency [Hz]";
                chartADC.ChartAreas[0].AxisY.Minimum  = 0;
                chartADC.ChartAreas[0].AxisY.Maximum  = 2000;
                chartADC.ChartAreas[0].AxisY.Interval = 400;
                chartADC.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                chartADC.ChartAreas[0].AxisY.MajorGrid.Interval      = 200;

                // Set a scale box
                tb_Ymax.Text = "0";
                tb_Ymin.Text = "-60";
                tb_Yint.Text = "10";

                // Create a new memory bitmap the same size as the picture box
                Bitmap bMap = new Bitmap((int)Math.Abs(PixelXmax - PixelXmin), (int)Math.Abs(PixelYmax - PixelYmin));
                bMap = ColorMapClass.Colorize(bMap, 255, JetColorMap);

                // Draw a backimage of Chart background
                if (chartADC.Images.Count != 0)
                {
                    chartADC.Images.RemoveAt(0);
                }

                chartADC.Images.Add(new NamedImage("ColorMapImage", bMap));
                chartADC.ChartAreas[0].BackImage         = chartADC.Images[0].Name;
                chartADC.ChartAreas[0].BackImageWrapMode = ChartImageWrapMode.Scaled;
            }
        }
Ejemplo n.º 3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a x-axis time data for Chart
            for (int n = 0; n < NByteTime / 2; n++)
            {
                TimeX[n] = n / Fs;
            }

            // Create a x-axis time data for Chart
            for (int n = 0; n < NFFT / 2; n++)
            {
                FreqX[n] = (float)Fs / NFFT * n;
            }

            // Calculate dBA Weighting
            // ref: https://en.wikipedia.org/wiki/A-weighting
            dBA_Weight[0] = 0; // DC components
            for (int n = 1; n < NFFT / 2; n++)
            {
                double FreqX2 = Math.Pow(FreqX[n], 2);

                double RA     = Math.Pow(12194, 2) * Math.Pow(FreqX2, 2);
                double RA_den = (FreqX2 + 20.6 * 20.6)
                                * Math.Sqrt((FreqX2 + 107.7 * 107.7)
                                            * (FreqX2 + 737.9 * 737.9))
                                * (FreqX2 + 12194 * 12194);
                RA           /= RA_den;
                dBA_Weight[n] = (float)(20 * Math.Log10(RA) + 2.0f);
            }

            // Initialize a chart setting
            chartADC.Text = "ADC";
            chartADC.ChartAreas[0].AxisX.Title    = "Time [sec]";
            chartADC.ChartAreas[0].AxisX.Minimum  = 0;
            chartADC.ChartAreas[0].AxisX.Interval = 0.05;
            chartADC.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chartADC.ChartAreas[0].AxisX.MajorGrid.Interval      = 0.05;
            chartADC.ChartAreas[0].AxisY.Title    = "ADC value";
            chartADC.ChartAreas[0].AxisY.Minimum  = -0.05;
            chartADC.ChartAreas[0].AxisY.Maximum  = 0.05;
            chartADC.ChartAreas[0].AxisY.Interval = 0.01;
            chartADC.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chartADC.ChartAreas[0].AxisY.MajorGrid.Interval      = 0.01;

            // Initialize a colormap
            JetColorMap = ColorMapClass.JetTable();
            //JetColorMap = ColorMapClass.Jet(); // Uncomment, if generation of a RGB Jet Table is wished

            // Get all serial port names
            string[] ArrayComPortsNames = SerialPort.GetPortNames();
            try
            {
                int    index       = -1;
                string ComPortName = null;

                do
                {
                    index += 1;
                    cboPorts.Items.Add(ArrayComPortsNames[index]);
                }while (!((ArrayComPortsNames[index] == ComPortName) ||
                          (index == ArrayComPortsNames.GetUpperBound(0))));

                Array.Sort(ArrayComPortsNames);

                // Get the first COM port for the text in the ComboBox
                if (index == ArrayComPortsNames.GetUpperBound(0))
                {
                    ComPortName = ArrayComPortsNames[0];
                }
                cboPorts.Text = ArrayComPortsNames[0];
            }

            catch (IndexOutOfRangeException err)
            {
                Console.WriteLine(err.Message);
                btnConnect.Enabled = false;
            }
        }