Sinewave with C#

Discussion in 'Programmer's Corner' started by FBorges22, Mar 31, 2009.

  1. FBorges22

    Thread Starter Active Member

    Sep 11, 2008
    104
    0
    Greetings,

    I am trying to write a program that generates an array to be used in the excel to represent a sinewave signal. The signal should have the following characteristics:

    Sampling Rate: 10000
    Number of Samples: 2500
    Amplitude: 170
    Frequency: 60Hz

    The image annexed in this post show how the program should works:

    And here is the code that is not working as it should be... What is wrong with him?

    Code ( (Unknown Language)):
    1. using System;
    2.  
    3. namespace Sinewave
    4. {
    5.     class Program
    6.     {
    7.         static double[] data = new double [2500];
    8.  
    9.         static void Main(string[] args)
    10.         {
    11.             for (int i = 0; i < 2500; i++)
    12.             {
    13.                 data[i] = 170 * Math.Sin(2 * Math.PI * 60 * i * 0.00004);
    14.                 Console.WriteLine(data[i].ToString());
    15.             }
    16.         }
    17.     }
    18. }[/i][/i]


    Thanks,
    FBorges22
     
  2. peajay

    Well-Known Member

    Dec 10, 2005
    67
    0
    Frequency generation works like this:

    Code ( (Unknown Language)):
    1. for (sample_number = 0; sample_number < number_of_samples; sample_number++) {
    2.   time_in_seconds = sample_number / sample_rate;
    3.   sample = amplitude * sin ( 2 * PI * frequency_in_hz * time_in_seconds );
    4. }
    In your code, where you multiply by 0.00004, that's effectively using a sample rate of 25000, which isn't the sample rate that you want.
     
  3. FBorges22

    Thread Starter Active Member

    Sep 11, 2008
    104
    0
    It worked...

    Thanks for the tip
     
Loading...