Author Topic: Turbo C - Capture SIN, COS wave with desired input and sampling frequency  (Read 1588 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Program:      Generate number of signals for SIN or COS wave based on input and sampling frequency.

Function:      The program generates a number of signals according to the user parameters

Example:      C:\>Generate   <Type> <N>  <frequency>  <Samplingfrequency>  < Filename>
   
         Where:
               Type:   Is the signal to generate as follows:
                  1:   Sine wave
                  2:   Cosine wave

         For example to generate 128 samples a sine wave of frequency 100Hz with sampling frequency of 800Hz and save the samples in a file named: Data.Txt;

         C:\>Generate 1  128  100  800  Data.Txt

User Interface:   The program should generate appropriate help and error messages. 


Code:
//Source Code Listing

#include <stdio.h>
#include <math.h>
#include <string.h>

int AddValue(const char *destFileName, float data)
{
   char buf[128];
   FILE *ostream = fopen(destFileName, "ab");
   if (ostream == 0)
      return -1;
   sprintf(buf, "%.6lf\r\n", data);
   fwrite(buf, 1, strlen(buf), ostream);
   fclose(ostream);
   return true;
}

static float global_angle_point; 
static int global_wave_type; 
static float global_input_frequency;
static int capture_started = 0;

int Start_Frequency_Wave(int type, float input_frequency)
{
   global_angle_point = 0;
   global_wave_type = type;
   global_input_frequency = input_frequency;
   capture_started = 1;
   return 1;
}

int Reset_Frequency_Wave()
{
   global_wave_type = -1;
   global_angle_point = 0;
   global_input_frequency = 0;
   capture_started = 0;
   return 1;
}

float Capture_Next_Wavepoint()
{
   // input_frequency = 100 Hz = 3.6 seconds for one sine wave
   // input_frequency = 200 Hz = 1.8 seconds for one sine wave
   // input_frequency = 300 Hz = 1.2 seconds for one sine wave
   // input_frequency = 360 Hz = 1.0 seconds for one sine wave
   if( capture_started != 1)
      return 0;

   float rad = 0;
   float data = 0;
   float factor = 100 / global_input_frequency;

   global_angle_point += factor;
   rad = 3.1417 / 180 * global_angle_point;
   if(global_wave_type == 1)
      data = sin(rad);
   else if(global_wave_type == 2)
      data = cos(rad);
   else
      return 0;
   return data;
}

int CaptureWave(int type, int numPoints, float input_frequency, float sampling_frequency, const char *filename)
{
   int i = 0;
   float j = 0; 
   float data = 0;
   float numTimes = 1;

   Start_Frequency_Wave(type, input_frequency);
   
   if(input_frequency < sampling_frequency)
   {
      // 100 Hertz input versus 500 Hertz sampling
      numTimes = sampling_frequency / input_frequency;
      // numTime = 5 - 5 times faster capture
   }
   else if(input_frequency > sampling_frequency)
   {
      // 500 Hertz input versus 100 Hertz sampling
      numTimes = sampling_frequency / input_frequency;
      // numTimes = 0.2 - 5 times slower capture
   }

   for(i = 0; i < numPoints; i++)
   {
      data = Capture_Next_Wavepoint();
      AddValue(filename, data);
      if(numTimes == 1)
         continue;
      if(numTimes > 1)
      {
         for(j = 1; j < numTimes; j += 1.0)
         {
            AddValue(filename, data);
            i++;
         }
      }
      else
      {
         for(j = numTimes; j < 1; j += numTimes)
         {
            data = Capture_Next_Wavepoint();
            // skip this value by delaying
         }
      }

   }
   return 1;
}

int main(int argc, char *argv)
{
   if(argc < 6)
   {
      printf("Syntax: Generate.exe <Type> <N>  <frequency>  <Samplingfrequency>  < Filename>");
      return -1;
   }
   CaptureWave(argv[1], argv[2], argv[3], argv[4], argv[5]);
   //CaptureWave(1, 2000, 100, 100, "C:\\t1.txt");
   //CaptureWave(1, 2000, 100, 1000, "C:\\t2.txt");
   //CaptureWave(1, 2000, 1000, 100, "C:\\t3.txt");
   return 0;
}


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1