1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <vector>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <array>
#include <complex>
#include <cmath>
using namespace std;
vector<double> calculate_mag_response(double start_freq, double end_freq,
int N, vector<double> num, vector<double> den);
int main(int argc,char *argv[])
{
vector<double> myNum, myDen, results;
//fstream outfile;
int N=100;
//outfile.open("datafile.txt");
myNum.push_back(1.0);
myNum.push_back(1.0); //create a smple zero at s=-1
myDen.push_back(1.0);
myDen.push_back(3.0);
myDen.push_back(2.0); //create two poles at s=-1,-2
results=calculate_mag_response(.1,1000,100,myNum,myDen); //Call the student function
//if (N=results.size()!= 100)
//{
//cout << "Your function did not provide correct number of data points. Please fix" << endl;
//exit(0);
//}
for (int k=0;k<=N-1;k++)
cout<<results[k]<<endl;
int r;
cin>> r;
//outfile << i << "\t" << results[i] << endl; //Write this garbage to a file
return(0);
}
vector<double> calculate_mag_response(double w0, double wf,
int N, vector<double> num, vector<double> den)
{
std::vector<double>result;
// double delta, freq, n, d;
//delta = (end_freq-start_freq)/(N-1);
// n=num.size();
///d=den.size();
for(double w=w0;w<=wf;w+=wf/N)
{ complex<double>s (0.0,w);
complex<double>num_result=0;
complex<double>den_result=0;
//freq=start_freq;
//for(int i=N;i>0;i--)
// {
for(unsigned int t=0;t<=num.size()-1;t++)
num_result+=num[t]*pow(s,num.size()-t-1);
//freq=start_freq;
// for(int i=N;i>0;i--)
// {
for(unsigned int i=0;i<=den.size()-1;i++)
den_result+=den[i]*pow(s,den.size()-1-i);
double G = 20*log10(abs(num_result/den_result));
result.push_back(G);
}
return (result);}
// cout << "num is " << num_result << endl << endl;
// cout << "den is " << den_result << endl << endl;
//return result;
// cout << result << " dB" << endl;
//system("pause");
//return (0);
|