plotting sine waves

closed account (SL0X92yv)
my question is to Create a program to add sines and cosines of frequencies and plot them visually.
Any idea as to how this question should be done .

help is appreciated
Last edited on
sin 0.2, cos 0.4 and cos 0.7 are real numbers, not functions.
closed account (SL0X92yv)
yes i know,but my problem is that how do i get to plot sine waves in the solution..
Imagine a rectangular area. This is the picture you're trying to draw. Each pixel will be either white or black. Each pixel is described by two numbers. The column it is in, with zero being the column on the far left, and the row it is in, with zero row being the one at the bottom.

In every column of pixels, one will be black. The rest will be white. Which pixel will be black? The pixel in row y, where y is sin (column value).

However, since sin(someNumber) ranges only from zero to one, we'll do ourselves a favour and call the bottom row zero, and the next row up row 0.01, and the next one 0.02, and so on.

So in column number zero, the black pixel is in the row numbered sin(zero).
In column number one, the black pixel is in the row numbered sin(1).
In column number two, the black pixel is in the row numbered sin(2).

Repeat for all columns. You now know which pixels to turn black. Create picture with 101 rows, with the bottom row being row zero, the next row being row 0.01, and so on. Picture should have as many columns as you like.

Remember that the cmath trig functions assume input is in radians. If you're working in degrees, adjust accordingly. If the image isn't precise enough for you, change the horizontal scale by, for example:

In column number zero, the black pixel is in the row numbered sin(zero).
In column number one, the black pixel is in the row numbered sin(0.1).
In column number two, the black pixel is in the row numbered sin(0.2).


Welcome to programming. The science, art and craft of thinking about problems in a way that the solutions lend themselves to being programmed, and the programming thereof. Programming is understanding problems and thinking about them. All the rest is memorising syntax.
closed account (SL0X92yv)

int main(int argc, char *argv[])
Last edited on
What library are you using to create and manipulate the images? Read the documentation for it.
You'll need to use a graphical library to do anything outside of the console. The console is text-only.

Examples of graphical libraries are OpenGL are DirectX. These interact directly with your video card (in fact, your video cards are built to support these). If you're using windows, Win32 will create the window that will display the OpenGL or DirectX. In linux I think it is X11.

Those are low-level and will be tough to master. You may want to use a wrapper to help such as SFML or SDL.

A higher-level framework that would be easier to work with is Qt. If you use the Qwt library that can plug into this, you can make some nice plots without the need to code much at all (maybe a hundred lines and that's IT!).


http://qwt.sourceforge.net/
Last edited on
If you just need to generate the jpeg image, and don't need to do anything fancy on screen, a simple image library would do the job; the image can be created in memory and written to file, and then you can use your choice of image viewing program to see it.
closed account (SL0X92yv)
I will be using imagemagick++.so any advise how to do it with imagemagick++?
1
2
3
4
5
6
7
8
9
10
11
#include <Magick++.h>
using namespace std; 
using namespace Magick; 
int main(int argc,char **argv) 
{ 
  InitializeMagick(*argv);
  Image image( "100x100", "white" ); 
  image.pixelColor( 49, 49, "red" ); // Set one pixel to colour red
  image.write( "red_pixel.jpg" ); 
  return 0; 
}


Lifted from the documentation: http://www.imagemagick.org/Magick++/Image.html
Last edited on
closed account (SL0X92yv)
i made changes in my pgm,,made a makefile and compiled it however how should I run it??do i need cmd line args??I tried image.display() but get an error..
what():wave:Access outside of image boundary..
Abort (core dumped)
It probably means that you wrote to a pixel outside of the image size.
You don't need a DirectX/OpenGL context if all you need is a graph. The API (Win32/whatever) should have some 2D drawing capability. DirectX/OpenGL are for hardware acceleration (though I suppose you could make yourself a pretty sweet looking graph :) )
Last edited on
closed account (SL0X92yv)
I still dont get a graph... :(
closed account (SL0X92yv)
any ways i also need a continuous graph to be displayed..if my img is 640*480,,i will have to calculate all the amplitude values for each "x" coordinate..so that will be a for loop like ...
for(x=0;X<640;x++)
but i need a different for loop inside that body to calculate each trig arg passed at the cmd line...eg
sin 0.2 cos 0.3 etc
how do i extract this from the cmd line??

ideas plz??
I haven't tested it (I don't have Magick++ installed). But I think this is what you're looking for. I like mixing waveforms. I'd like to know if it works.

I've made the assumption that your arguments are phase offsets on their respective . They could also be amplitudes on the gain but you didn't specify.

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
#include <Magick++.h> // Magick::InitializeMagick(), Magick::Image, Magick::Geometry
#include <cstdlib>    // atof()
#include <cmath>      // sin(), cos()
#inlcude <iostream>   // std::cout, std::endl

const double pi = 3.14159265359;
const int X = 640;
const int Y = 480;

int main(int argc,char *argv[]) 
{ 
    if ((argc < 2) || (argc%2==0)) // If too few arguments, or an even number of arguments, quit
    {
        std::cout << "Usage: wave-type phase [wave-type][phase] ..." << std::endl 
                  << " Valid types are sin or cos "                  << std::endl;
        return -1;
    }
    
    double output[X] = {0}; // Contains an Y value for each pixel in the X axis;
    int nb_waves = (argc-1)/2;  // number of waves
    
    // let's calculate the Y values for X=[0,4pi].
    for (int i = 0; i < nb_waves; ++i)
    {
        double scalar = 4. * pi / (double)X; // This will scale the X value from [0,X] to [0,4pi], each X-pixel is 0.01963 radians
        double phase = atof(argv[2*i+2]);    // This is the phase offset in radians as defined by the command line
        
        if (argv[2*i+1] == "cos")
        {
            for (j = 0; j < X; ++j)
            {
                output[j] += cos(j * scalar + phase); // cos(x + phase) from 0 to 4pi
            }
        }
        else // argv[2i+1] == "sin", we'll assume
        {
            for (j = 0; j < X; ++j)
            {
                output[j] += sin(j * scalar + phase); // sin(x + phase) from 0 to 4pi
            }
        }
    }

    // We have our waves added, now lets normalize them to the Y axis:
    // We now have waves between +- (argc-1) / 2  (if there were 3 waves, between +- 3.0).
    for (int i = 0; i < X; ++i)
	{
        output[i] *= (double)Y / ((double)nb_waves*2.);  // now it's [-240,+240] (we can convert it to pixels easily now
        output[i] += X/2;                                // now it's [0,480]
    }

    // Draw your graph
    Magick::InitializeMagick(*argv);
    Magick::Image image( Magick::Geometry(X,Y, 0, 0), "white" );
    for (int i = 0; i < X; i++)
    {
        image.pixelColor( i, (int)output[i], "red");
    }
    image.write( "red_pixel.jpg" ); 
    
    return 0; 
}
Last edited on
Topic archived. No new replies allowed.