CODE
using System;
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 System.Numerics; // complex number format
using MathNet.Numerics; // Library for
using MathNet.Numerics.IntegralTransforms; // FFT
namespace LiveChart_App
{
public partial class Form1 : Form
{
#region GLOBALS
static int numSamples = 1000;
static int sampleRate = 2048;
// INITIAL VALUES OF A CLASS TO GO ..
int secondHarm = 1;
int thirdHarm = 1;
double secondPH = 0;
int thirdPH = 0;
double f1 = 60;
double f2 = 120;
double f3 = 180;
#endregion
// complex variables ...
Complex[] samples = new Complex[numSamples];
#region GENERATOR and FFT
public void generateWave(int secondHarm, int thirdHarm, double secondPH, int thirdPH)
{
chart1.Series["Waveform"].Points.Clear();
chart1.Series["Second Harmonic"].Points.Clear();
chart1.Series["Third Harmonic"].Points.Clear();
// generate the waves
double[] fundamental = Generate.Sinusoidal(numSamples, sampleRate, f1, 10.0, 0.0, 0);
double[] second = Generate.Sinusoidal(numSamples, sampleRate, f2, secondHarm, 0.0, secondPH);
double[] third = Generate.Sinusoidal(numSamples, sampleRate, f3, thirdHarm, 0.0, thirdPH);
for (int i = 0; i < numSamples; i++)
{
samples[i] = new Complex(fundamental[i] + second[i] + third[i], 0);
}
// plot composite waveform
for (int i = 0; i < samples.Length / 5; i++)
{
double time = ((i + 1.0) / numSamples) / 2;
if (checkBox1.Checked)
{
chart1.Series["Second Harmonic"].Points.AddXY(time, second[i]);
}
if (checkBox2.Checked)
{
chart1.Series["Third Harmonic"].Points.AddXY(time, third[i]);
}
chart1.Series["Waveform"].Points.AddXY(time, samples[i].Real);
chart1.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Arial", 12.0f);
chart1.ChartAreas["ChartArea1"].AxisX.Title = "Seconds";
}
}
public void PlotFFT()
{
chart2.Series["Frequency"].Points.Clear();
// FFT
Fourier.Forward(samples, FourierOptions.NoScaling);
for (int i = 1; i < samples.Length / 10; i++)
{
chart2.ChartAreas["ChartArea1"].AxisX.Title = "Hz";
chart2.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Arial", 12.0f);
chart2.ChartAreas["ChartArea1"].AxisX.MinorTickMark.Enabled = true;
// Get the magnitude of each FFT sample:
// 2* absolute[sqrt(r^2 + i^2)]/N // single sided spectrm ...
double mag = (2.0 / numSamples) * (Math.Abs(Math.Sqrt(Math.Pow(samples[i].Real, 2)
+ Math.Pow(samples[i].Imaginary, 2))));
// frequency resolution
double dHz = sampleRate / numSamples;
chart2.Series["Frequency"].Points.AddXY(dHz * i, mag);
}
}
#endregion
// PASS slider values to FFT
void UIcheckBoxChecker()
{
generateWave(trackBar1.Value, trackBar2.Value, trackBar3.Value, trackBar4.Value);
#region PHASE SLIDERS
double u = 2.0 * Math.PI * (trackBar3.Value / (double)trackBar3.Maximum);
double phi2 = u * 360.0 / (2 * Math.PI);
double v = 2.0 * Math.PI * (trackBar4.Value / (double)trackBar4.Maximum);
double phi3 = v * 360.0 / (2 * Math.PI);
lblPh2.Text = phi2.ToString("F1");
lblPh3.Text = phi3.ToString("F1");
}
public Form1()
{
InitializeComponent();
}
// FIRST CLICK on BUTTON
//
//
//
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Time Domain... ";
generateWave(secondHarm, thirdHarm, secondPH, thirdPH);
PlotFFT();
///
/// TRACKBARS
///
///
///
#region AMP/PHASE SLIDERS
void trackBar1_Scroll(object sender, EventArgs e)
{
label2.Text = Convert.ToString(trackBar1.Value);
UIcheckBoxChecker();
PlotFFT();
}
void trackBar2_Scroll(object sender, EventArgs e)
{
label3.Text = Convert.ToString(trackBar2.Value);
UIcheckBoxChecker();
PlotFFT();
}
void trackBar3_Scroll(object sender, EventArgs e)
{
UIcheckBoxChecker();
PlotFFT();
}
void trackBar4_Scroll(object sender, EventArgs e)
{
UIcheckBoxChecker();
PlotFFT();
}
#endregion
#region FREQ SLIDERS
void trackF1_Scroll(object sender, EventArgs e)
{
lblFreq1.Text = trackF1.Value.ToString("F1");
f1 = trackF1.Value;
UIcheckBoxChecker();
PlotFFT();
}
void trackF2_Scroll(object sender, EventArgs e)
{
lblFreq2.Text = trackF2.Value.ToString("F1");
f2 = trackF2.Value;
UIcheckBoxChecker();
PlotFFT();
}
void trackF3_Scroll(object sender, EventArgs e)
{
lblFreq3.Text = trackF3.Value.ToString("F1");
f3 = trackF3.Value;
UIcheckBoxChecker();
PlotFFT();
}
}
}
}