Amplitude & frequency (Wave modulation) C# Program

Mercy Vara - Nov 6 - - Dev Community

*Amplitude and Frequency (Wave Modulation) *
(https://www.youtube.com/watch?v=H0KBVZBsI7A&feature=youtu.be)

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;

namespace Wave_Modulation_Amplitude_and_Frequency
{
public partial class Form1 : Form
{
private Timer timer1;
private Button buttonStart;
private Button buttonPause;
private Ball ball;
private const float ballSize = 9;
private double time;
private bool isPaused = false;

    private TrackBar HSlider_Amp;
    private TrackBar HSlider_Freq;
    private Label labelAmp;
    private Label labelFreq;
    private TextBox textBoxAmp;
    private TextBox textBoxFreq;
    private float amplitude = 50f; // Default amplitude
    private float frequency = 1f; // Default frequency in Hz

    public Form1()
    {
        InitializeComponent();
        InitializeButton();
        InitializeSlidersAndControls();

        this.Width = 1300;
        this.Height = 700;
        this.Text = "Amplitude and Frequency (Wave Modulation)";
        this.BackColor = Color.Black;
        this.CenterToScreen();

        timer1 = new Timer
        {
            Enabled = false,
            Interval = 20 // Update every 20ms
        };
        timer1.Tick += Timer1_Tick;
        this.Paint += Form1_Paint;

        ball = new Ball(Color.Red, 0, false); // Red ball follows a sine wave
    }

    private void InitializeButton()
    {
        buttonStart = new Button
        {
            Text = "Start",
            AutoSize = true,
            Location = new Point(1070, 60),
            ForeColor = Color.Red,
            BackColor = Color.Yellow,
            Font = new Font("Microsoft Sans Serif", 16, FontStyle.Bold)
        };
        buttonStart.Click += new EventHandler(ButtonStart_Click);
        Controls.Add(buttonStart);

        buttonPause = new Button
        {
            Text = "Pause",
            AutoSize = true,
            Location = new Point(1150, 60),
            ForeColor = Color.Red,
            BackColor = Color.Yellow,
            Font = new Font("Microsoft Sans Serif", 16, FontStyle.Bold)
        };
        buttonPause.Click += new EventHandler(ButtonPause_Click);
        Controls.Add(buttonPause);
    }

    private void ButtonPause_Click(object sender, EventArgs e)
    {
        if (isPaused)
        {
            // Resume the timer
            timer1.Start();
            buttonPause.Text = "Pause"; // Update button text to show current state
            isPaused = false;
        }
        else
        {
            // Pause the timer
            timer1.Stop();
            buttonPause.Text = "Resume"; // Update button text to show current state
            isPaused = true;
        }
    }

    private void InitializeSlidersAndControls()
    {
        int sliderWidth = (int)(this.ClientSize.Width * 0.4); // 40% of form width

        // Amplitude Slider
        HSlider_Amp = new TrackBar
        {
            Orientation = Orientation.Horizontal,
            Minimum = 10,
            Maximum = 200,
            Value = (int)amplitude,
            TickFrequency = 10,
            Location = new Point(190, 10),
            Width = 500
        };
        HSlider_Amp.Scroll += (s, e) =>
        {
            amplitude = HSlider_Amp.Value;
            textBoxAmp.Text = amplitude.ToString();
        };
        Controls.Add(HSlider_Amp);

        // Frequency Slider
        HSlider_Freq = new TrackBar
        {
            Orientation = Orientation.Horizontal,
            Minimum = 0,
            Maximum = 1000,
            Value = (int)(frequency * 100), // scale frequency for slider
            TickFrequency = 100,
            Location = new Point(750, 10),
            Width = 500
        };
        HSlider_Freq.Scroll += (s, e) =>
        {
            frequency = HSlider_Freq.Value;
            textBoxFreq.Text = frequency.ToString();
        };
        Controls.Add(HSlider_Freq);

        // Amplitude Label
        labelAmp = new Label
        {
            Text = "Amplitude (10 - 200 volts): ",
            AutoSize = true,
            ForeColor = Color.White,
            BackColor = Color.Black,
            Location = new Point(200, 60),
            Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold)
        };
        Controls.Add(labelAmp);

        // Frequency Label
        labelFreq = new Label
        {
            Text = "Frequency (0 - 1000 Hz):",
            AutoSize = true,
            ForeColor = Color.White,
            BackColor = Color.Black,
            Location = new Point(760, 60),
            Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold)
        };
        Controls.Add(labelFreq);

        // Amplitude TextBox
        textBoxAmp = new TextBox
        {
            Text = amplitude.ToString(),
            ForeColor = Color.White,
            BackColor = Color.Black,
            Location = new Point(455, 60),
            Width = 50,
            Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold),
            ReadOnly = true
        };
        Controls.Add(textBoxAmp);

        // Frequency TextBox
        textBoxFreq = new TextBox
        {
            Text = frequency.ToString(),
            ForeColor = Color.White,
            BackColor = Color.Black,
            Location = new Point(1000, 60),
            Width = 60,
            Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold),
            ReadOnly = true
        };
        Controls.Add(textBoxFreq);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Calculate the middle points
        int midY = this.ClientSize.Height / 2;
        int midX = 50; // Left margin for Y-axis

        // Draw X-Axis (Frequency)
        e.Graphics.DrawLine(Pens.White, 0, midY, this.ClientSize.Width, midY);
        e.Graphics.DrawString("Frequency", new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.White, this.ClientSize.Width - 100, midY + 10);

        // Draw Y-Axis (Amplitude)
        e.Graphics.DrawLine(Pens.White, midX, 0, midX, this.ClientSize.Height);
        e.Graphics.DrawString("Amplitude", new Font("Microsoft Sans Serif", 10, FontStyle.Bold), Brushes.White, midX + 5, 10);

        // Draw the red ball and its trajectory
        if (ball.Trajectory.Count >= 2)
        {
            using (Pen trajectoryPen = new Pen(ball.Color, 2f))
            {
                e.Graphics.DrawLines(trajectoryPen, ball.Trajectory.ToArray());
            }
        }

        // Draw the ball
        e.Graphics.FillEllipse(new SolidBrush(ball.Color), ball.Position.X, ball.Position.Y, ballSize, ballSize);
    }


    private void Timer1_Tick(object sender, EventArgs e)
    {
        time += 0.02; // Time increment
        ball.UpdatePosition(time, this.ClientSize.Height / 2, ballSize, amplitude, frequency);
        this.Invalidate(); // Trigger repaint
    }

    private void ButtonStart_Click(object sender, EventArgs e)
    {
        ball.Reset(this.ClientSize.Height / 2 - ballSize / 2);
        timer1.Start(); // Start the timer
    }
}

public class Ball
{
    public Color Color { get; }
    public PointF Position { get; private set; }
    public List<PointF> Trajectory { get; }
    private float phaseShift;
    private bool isStraight;

    public Ball(Color color, float phaseShift, bool isStraight)
    {
        Color = color;
        this.phaseShift = phaseShift;
        this.isStraight = isStraight;
        Trajectory = new List<PointF>();
    }

    public void UpdatePosition(double time, float midY, float ballSize, float amplitude, float frequency)
    {
        float speed = 5f;

        Position = new PointF(
            Position.X + speed,
            midY + (float)(amplitude * Math.Sin(2 * Math.PI * frequency * time + phaseShift))
        );

        Trajectory.Add(new PointF(Position.X, Position.Y));

        if (Position.X > 1300)
        {
            Position = new PointF(0, midY); // Reset to start position
            Trajectory.Clear(); // Clear the trajectory points
        }
    }

    public void Reset(float initialY)
    {
        Position = new PointF(0, initialY);
        Trajectory.Clear();
    }
}
Enter fullscreen mode Exit fullscreen mode

}

.