Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment we will learn more about frequency response by writing a C++

ID: 2990156 • Letter: I

Question

In this assignment we will learn more about frequency response by writing a C++ function. Your function

should have the following prototype:

std::vector<double> calculate_mag_response(double start_freq, double end_freq,

int N, std::vector<double> num, std::vector<double> den)

The function should return a vector size

N

of magnitude responses in decibels of the transfer function

dened by numerator and denominator vectors num and den.

I will test your function with a driver program call it say main.cpp, similar to the following. To adequately

test, you should drive this with more transfer functions.

#include <vector>

#include <iostream>

#include <cstdlib>

#include <ifstream>

using namespace std;

int main(int argc,char *argv[])

{

vector<double> myNum, myDen, results;

ifstream outfile;

int N;

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 i=0;i<N;i++)

outfile << i << " " << results[i] << endl; //Write this garbage to a file

return(0);

}

Explanation / Answer

#include <vector>

#include <iostream>

#include <cstdlib>

#include <fstream>

#include <math.h>
using namespace std;

std::vector<double> calculate_mag_response(double start_freq, double end_freq, int N, std::vector<double> num, std::vector<double> den)
{
    int count=0;
    //------------------FOR FREQUENCY ARRAY -------------------//
    double freq_step =(end_freq-start_freq)/(N-1);      // calculate step size for N; Formula is (end_freq-start_freq)/(N-1)
    std::vector<double> freq;
    std::vector<double> result;
    freq.push_back(start_freq);                         //insert start_freq into frequency vector
    for(int i=1;i<N;i++)
        freq.push_back(start_freq+i*freq_step);         //insert frequency with increment with step size
    //--------------------------------------------------------//
    //-----------FOR REAL AND IMAGINARY COEFFICIENT ----------//
    std::vector<double> den_re;
    std::vector<double> den_img;
    std::vector<double> num_re;
    std::vector<double> num_img;
    double omega=0;
    //-------------------Denominator-------------------------------------
    while(!den.empty())                             // Seperate Imaginary and Real Coefficent From the Denominator
    {
        if(count%2==0)                              // for every polynomial when s=jw then even power of s coefficents are real
        {
            if(count%4==0)                          // For Real coefficient s^0 have +ve sign, s^2 -ve sign
                den_re.push_back(den.back());
            if(count%4==2)
                den_re.push_back(-den.back());
            den.pop_back();
        }
        else                                        // for every polynomial when s=jw then odd power of s coefficents are real
        {
            if(count%4==1)                          // For Real coefficient s^1 have +ve sign, s^3 -ve sign
                den_img.push_back(den.back());
            if(count%4==3)
                den_img.push_back(-den.back());
            den.pop_back();
        }
        count++;
    }
    count=0;
    //-------------------Numerator-------------------------------------
    while(!num.empty())
    {
        if(count%2==0)
        {
            if(count%4==0)
                num_re.push_back(num.back());
            if(count%4==2)
                num_re.push_back(-num.back());
            num.pop_back();
        }
        else
        {
            if(count%4==1)
                num_img.push_back(num.back());
            if(count%4==3)
                num_img.push_back(-num.back());
            num.pop_back();
        }
        count++;
    }
    //--------------------------------------------------------//
    for(int i=0;i<freq.size();i++)
    {
        //-----------------------CALCULATION FOR DENOMINATOR MAGNITUDE COEFFICIENT-------------------------
        double sum_re=0;
        double sum_img=0;
        double sum_den;
        double sum_num;
        double num_den;
        for(int j=0;j<den_re.size();j++)
        {
            double comp1=den_re[j]*pow(freq[i],(2*j));      //coefficient * power of Frequency term for Real
            sum_re+=comp1;
        }
        sum_re=pow(sum_re,2);                               // Final Real Term Square
        for(int j=0;j<den_img.size();j++)
        {
            double comp1=den_img[j]*pow(freq[i],(2*j+1));   //coefficient * power of Frequency term for Imaginary
            sum_img+=comp1;
        }
        sum_img=pow(sum_img,2);                             // Final Imaginary Term Square
        sum_den=sqrt(sum_re+sum_img);
        //-----------------------CALCULATION FOR NUMERATOR COEFFICIENT-------------------------
        sum_re=0;
        sum_img=0;
        for(int j=0;j<num_re.size();j++)
        {
            double comp1=num_re[j]*pow(freq[i],(2*j));
            sum_re+=comp1;
        }
        sum_re=pow(sum_re,2);
        for(int j=0;j<num_img.size();j++)
        {
            double comp1=num_img[j]*pow(freq[i],(2*j+1));
            sum_img+=comp1;
        }
        sum_img=pow(sum_img,2);
        sum_num=sqrt(sum_re+sum_img);


        num_den=sum_num/sum_den;                        // Numerator Magnitude with Frquency divide Numerator Magnitude with Frquency
        result.push_back(num_den);
        //cout<<"Num : "<<sum_num<<" Den : "<<sum_den<<" Mag : "<<result[i]<<endl;
    }
    return result;
}
int main(int argc,char *argv[])

{

vector<double> myNum, myDen, results;

ofstream outfile;       // ifstream outfile; This is Wrong By Professor

int N;

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 i=0;i<results.size();i++)

outfile << i << " " << results[i] << endl; //Write this garbage to a file

return(0);

}