Your task is to write a program that will draw graphs and output them as a full colour image
in the .ppm file format and display them using a suitable external image viewer (IrfanView).
The program will use code generated sine waves, and harmonics of these, to calculate and
display the components of a square wave.
You will need to write a program that allows the user to specify how many harmonic
components to include in the plot. Your Signals and Systems or Mathematics knowledge
should enable you to make the appropriate calculations.
The data from each harmonic should be displayed - in a different colour - and the resultant
waveform also displayed. The resultant signals should be output as a single file in the .ppm
format. Assume an image 256 pixels wide by 128 pixels high with a colour depth of 8 bits for
each channel. There should be two full cycles of the waveform under construction across the
width of the image. The background colour should be black, and any axes or labels you add
should be in white. Check the resulting file, using the image viewer IrfanView.
Design a user interface allowing the user to specify the waveform they wish to construct and
the number of harmonic components to use. The user should also be able to re-run the
calculation without exiting the program.
The code containing your main() function should be in a file called ela010task1.c
Any functions you write should be implemented in a separate C source file in your project,
called functions.c. You will need to write a prototypes file, functions.h which
should be #included at the top of functions.c and ela010task1.c

Files :
- Source files functions.c, functions.h, ela010task2.c
- Executable file ela010task2.exe.

Recommended Answers

All 2 Replies

This is the code we have created


#include <stdio.h>
#include <math.h>

#define W 1024
#define H 512
#define RED 0
#define GREEN 1
#define BLUE 2

int image[H][W][3];

    int y, x, g, p;         /*g = number of harmonics requested by user.
                            p is the number of harmonics that will be used to create the square wave. */
    FILE *outfile = NULL; 
    for (y = 0; y < H; y++)
    for (x = 0; x < W; x++)
        image[y][x][RED] = 0;
        image[y][x][GREEN] = 0;         /*Generates black background */
        image[y][x][BLUE] = 0;  

    for (y = 0; y < H; y++)
        image[y][0][RED] = 255; 
        image[y][0][GREEN] = 255;       /*Generates Y axis*/
        image[y][0][BLUE] = 255;

    for (x = 0; x < W; x++)
        image[H/2][x][RED] = 255;
        image[H / 2][x][GREEN] = 255;   /*Generates X axis*/
        image[H/2][x][BLUE] = 255;

    printf("How many harmonics would you like?");
    scanf("%i", &g);
    p = g * 2 - 1;          /*Generates correct number of odd harmonics*/
    if (g < 1)
    printf("Insufficient number of harmonics!");

        for (x = 0; x < W; x++)
            y = H / 2 + 255 * (sin(-0.00614*x*p) / p);
            image[y][x][RED] = 0;
            image[y][x][GREEN] = 255;
            image[y][x][BLUE] = 0;
    while (p >= 1);
        outfile = fopen("myfile.ppm", "w");
        fprintf(outfile, "P3\n# %s\n%d %d\n255\n", "myfile.ppm", W, H);
        for (y = 0; y < H; y++)
        for (x = 0; x < W; x++)
            fprintf(outfile, "%d %d %d\n", image[y][x][RED], image[y][x][GREEN], image[y][x][BLUE]);
        return 0;

You haven't declared/implemented your functions that handle the computations and display output. Where is the functions.h header and functions.c code? You will also have to #include "functions.h" in the ela010task2.c file where main() should be. In addition, main() should be int main(void). There are other likely problems with your current code but this will start.

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.