Hi,

I have written a small program to calculate Fourier coefficients of square wave.

#include<iostream.h>
#include<stdlib.h>

int main()
{
    int *A,*B,a,n,i,;
    
    float pi=3.14;

    cout<<"Enter value of amplitude";
    cin>>a;    
    cout<<"Enter which harmonic you want to display";
    cin>>n;
    
    A=new int[n+1];
    B=new int[n+1];
    
    A[0]=0;
    
    for(i=0;i<=n;i++)
    {
                    B[i]=0;
    }
    for(i=2;i<=n;i=i+2)
    {
                       A[i]=0;
    }
    for(i=1;i<=n;i=i+4)
    {                  
                           
                       A[i]=(4*a)/(i*pi);
                       A[i+2]= -(4*a)/(i*pi);
                       
    }
    for(i=0;i<=n;i++)
    {
                   cout<<A[i]<<"\t";
    }
    cout<<endl;
    for(i=0;i<=n;i++)
    {
                   cout<<B[i]<<"\t";
    }

     system("pause");
     return(0);
   
}

I need to plot it using graphics functions.

Please help me with it.

Thanks.

Regards,
Harleen

Recommended Answers

All 11 Replies

Graphics aren't a standard part of the C++ language; in order to help, we'd need to know what graphics library you were using, and which compiler and OS you were working in as well.

I am using dev c++
windows 7
and graphics.h

Also, I would recommend separating the calculation out into a separate function, like this:

void FourierSquareWave(int& *A, int& *B, int n)
{
    int a, b, i;

    A[0]=0;
    
    for(i=0;i<=n;i++)
    {
        B[i]=0;
    }
    for(i=2;i<=n;i=i+2)
    {
        A[i]=0;
    }
    for(i=1;i<=n;i=i+4)
    {                  
                           
        A[i]=(4*a)/(i*pi);
        A[i+2]= -(4*a)/(i*pi);             
    }
}

OK, so the code I posted a while back isn't working, now that I've tested it; however, I have a working function now for this purpose, though I have changed some details about. Before I post it, though, let me ask this: is the B array even necessary? All that you do with it in the original is clear it; the values aren't used anywhere except in the print-out. Why is it there?

Beyond that, I would recommend replacing Dev-C++ with a more current IDE such as Code::Blocks. Not only is Dev-C++ several years out of date - it hasn't been updated since 2005 - but also Code::Blocks fixes a number of problems with Dev-C++, such as the need to add the pause at the end of a console program. It also integrates support for wxWidgets, a very useful windowing and graphics library.

Finally you really ought to replace the older <iostream.h> and <stdlib.h> references with <iostream> and <cstdlib>, which are the newer standard versions of the headers. Note that you will need to add a 'using namespace std;' line as well if you do this.

Schoil-R-LEA...Thanks a lot..

Yes, you are right about what you are saying with B array. It is just showing the Fourier coefficients.

But my problem is still unaddressed. I need to plot the wave.

OK, just to clarify things: the <graphics.h> header provided with Dev-C++ is actually part of a package called WinBGIm, which is essentially a port of the older Borland Graphics Interface package to MinGW. There is a page explaining the functions in the library here. I'll try to see if I can get it to work with Code::Blocks as well (it ought to from what I've read) and try figuring out things so I can help you some more.

This article is probably the closest thing to what you are looking for that I've found. If you need further help, this page discusses the setup of the BGI library in Dev-C++.

If you decide to use Code::Blocks instead, the directions for setting up the libraries can be found here. The process is fairly simple and straightforward, though you need to include all of the libraries mentioned in the Dev-C++ article, not just GDI32. If you need further help with it, just ask me.

Use matlab. Its made for these kinds of things. Choose the correct language.

I have to agree with firstPerson, assuming that this is an option for you. MatLab is much better suited for what you're trying to do.

That having been said, here's the code I've been able to come up with so far:

#include <iostream>
#include <cstdlib>
#include <graphics.h>

using namespace std;

void FourierSquareWave(int *A, int amplitude, int harmonic);
void plotFourier(int *A, int n);


int main()
{
    int i, n, a;
    int *A;

    cout<<"Enter value of amplitude ";
    cin>>a;
    cout<<"Enter which harmonic you want to display ";
    cin>>n;

    A=new int[n+1];

    FourierSquareWave(A, a, n);

    for(i = 0; i <= n; i++)
    {
        cout<< A[i] << "\t";
    }
    cout << endl;

    plotFourier(A, n);

    delete A;
    return(0);
}


void FourierSquareWave(int *A, int amplitude, int harmonic)
{
    const double pi = 3.1415;

    A[0] = 0;

    for(int i = 2; i <= harmonic; i += 2)
    {
        A[i] = 0;
    }

    for(int j = 1; j <= harmonic; j += 4)
    {
        A[j] = (4 * amplitude) / (j * pi);
        A[j + 2] = -(4 * amplitude) / (j * pi);
    }
}

void plotFourier(int *A, int n)
{
    int gDriver = DETECT, gmode, errorcode;
    initgraph(&gDriver, &gmode, "");

    /* read result of initialization */
    errorcode = graphresult();

    if (errorcode != grOk)   /* an error occurred */
    {

        std::cout << "Graphics error: %s " << grapherrormsg(errorcode) << std::endl;
        std::cout << "Press any key to halt:";
        getch();
        exit(1); /* terminate with an error code */

    }

    initwindow(800, 600, "Fourier Square-Wave Approximation");

    for (int i = 0; i < n; i++)
    {
        putpixel(i, A[i] * 10 + 100, 16776960);
    }

    while(!kbhit())
    {
    }

    closegraph();
}

It does not plot the function correctly, but I expect that with some work you'll find a way to do it, if you don't want to or can't use MatLab instead.

@Schoil-R-LEA

Before posting it in this forum , I had tried the same thing.

I think that Fourier coefficients have no relation with plotting wave, there must be algorithms for plotting waves(like bresenham for circle) or there could be some predefined functions(for which I have no idea).

two options for plotting C++ is using excel and tecplot. in excel the file must be saved in .txt but the resulting graph is quite rough. using tecplot is better option in fact for all plotting purpose. it is to save data in .dat format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.