Sunteți pe pagina 1din 3

// // // //

ExNo4/workshop number 3 (1) Michael Benhamou Generate 128 samples at frequency 1000Hz and sample rate of 8kHz. DFT function determines the Mean Square Power of the signal the result is in a text file //Header files

#include "stdafx.h" #include <cmath> #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #define Fs 8000 #define f 1000 #define theta 0.0 #define A 1.0 #define PI 3.14159 #define N 128 #define Cycles 1 using namespace std;

//Defines the sampling frequency //Defines the frequency //Defines the phase //Defines the constant for Amplitude //Defines PI as a constant //Number of samples //Number of Cycles

void CreateFile(double *File_Data); //function prototypes void SinWav(double *x, int A1, double fa, double FreSa, double phase, int c, int N1); //function prototypes void DftFunc(double *Mag, int Amp, double *Data); //function prototypes int main(int argc, char* argv[]) { int n; double SinData[N]; amples double DftData[N]; ude Samples // Array to store the DFT Magnit // Array to store the sinewave S

SinWav(SinData,A,f,Fs,theta,Cycles,N); //Generates the sinewave signal // Ask the user to enter a Filename cout<<"Please Enter a Filename for Sampled Sinewave (No Space):"<<endl; CreateFile(SinData); DftFunc(DftData,N,SinData); //Creates the DFT

// Ask the user to enter a Filename cout<<"Please Enter a Filename for DFT of the Sinewave (No Space):"<<end l; CreateFile(DftData); nitude spectrum return 0; } void SinWav(double *x, int A1, double f1, double fs, double phase, int c, int N1 ) //Generate sinewave. { int n; for(n=0; n<N1; n++) { x[n]=A1*sin((2*PI*f1/fs*c*n)+phase); } //Saves the Mean Square Power or mag

} //Function to determine the DFT void DftFunc(double *Mag, int SampNum, double *Data) { double Real, Imag; int n, k; cout<<setiosflags(ios::left); stified cout<<setw(16)<<"Sample no."; cout<<setw(12)<<"Real"; cout<<setw(13)<<"Imaginary"; cout<<setw(16)<<"Mean Square Power"<<'\n'; cout<<"----------------------------------------------------------"<<'\n' ; cout<<resetiosflags(ios::left); // Resets the justification for(k=0; k<SampNum; k++) { Real=Imag=0.0; for(n=0; n<SampNum; n++) { Real+=Data[n]*cos(2*PI*k*n/SampNum); Imag+=Data[n]*sin(2*PI*k*n/SampNum); } Mag[k]=pow(Real,2)+pow(Imag,2); // Now Print The Results cout<<setprecision(2); // Sets the number of decimal points cout<<setiosflags(ios::fixed); // Sets Number format to fixed po int cout<<setw(6)<<k; cout<<setw(14)<<Real; cout<<setw(14)<<Imag; cout<<setw(14)<<Mag<<"\n"; } cout<<"----------------------------------------------------------"<<'\n' ; } //Function to save sample data in a text file void CreateFile(double *File_Data) { int n; char File_Name[40]; // Array to store the filename // Now save this onto a text file. cin>>File_Name; // Get the User Respond std::ofstream fp(File_Name); // Open a Text file using the pointer fp for(n=0; n<N; n++) { fp<<File_Data[n]<<'\n'; // Store the samples //Prepares the output format as left ju

} }

S-ar putea să vă placă și