Saturday 18 November 2017

C++ Program to Compute DFT Coefficients Directly


Code:

#include  iostream
#include  math.h

using namespace std;

#define PI 3.14159265

class DFT_Coefficient
{
    public:
        double real, img;
        DFT_Coefficient()
        {
            real = 0.0;
            img = 0.0;
        }
};
int main(int argc, char **argv)
{
    int N = 10;
    cout << "Calculation DFT Coefficients\n";
    cout << "Enter the coefficient of simple linear function:\n";
    cout << "ax + by = c\n";
    double a, b, c;
    cin >> a >> b >> c;

    double function[N];
    for (int i = 0; i < N; i++)
    {
        function[i] = (((a * (double) i) + (b * (double) i)) - c);
        //System.out.print( "  "+function[i] + "  ");
    }

    cout << "Enter the max K value: ";
    int k;
    cin >> k;

    double cosine[N];
    double sine[N];

    for (int i = 0; i < N; i++)
    {
        cosine[i] = cos((2 * i * k * PI) / N);
        sine[i] = sin((2 * i * k * PI) / N);
    }

    DFT_Coefficient dft_val;
    cout << "The coefficients are: ";

    for (int i = 0; i < N; i++)
    {
        dft_val.real += function[i] * cosine[i];
        dft_val.img += function[i] * sine[i];
    }
    cout << "(" << dft_val.real << ") - " << "(" << dft_val.img << " i)";

}

Output:

Calculation DFT Coefficients
Enter the coefficient of simple linear funtion:
ax + by = c
1 2 3
Enter the max K value: 
2
The coefficients are: (-15) - (-20.6457 i)

------------------
(program exited with code: 0)
Press return to continue



More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...