int main() { /* give range of X axis, then define Y as x^2 + 3x + 3 */ plotdata x(-5.0, 2.0), y = x*x + 3*x + 3; /* Plot the graph */ plot(x, y); /**********************************************************/ /* Points may be entered one at a time into the axes data */ /* .. a "point-by-point" data entry method. */ /* Erase previous data in axes X and Y so they can be re-used.*/ clear(x); clear(y); /* * Enter each curve point as a coordinate pair using the plotdata * insertion operator "<<" ( or use the "point" function instead). */ for (int i = -180 ; i <= 180; i++) { double y_value = i / 180.0 + cos(DEG_TO_RAD * i); x << i; y << y_value; /* "<<" inserts a new value into the plot data. * You could instead use the point function, with * point(x, y, x_value, y_value); if you prefer. */ } /* Graph drawn in blue colour (Each data point is joined to * the preceding and following points, forming the curve.). */ plot(x, y, BLUE); /**********************************************/ /* Plot function y = sin(x) between 0 and 360 */ x = plotdata(0.0, 360.0); y = sin(x * DEG_TO_RAD); /* Give the function name to be printed as the window label */ plot(x, y, CRIMSON, "sin(x)"); /*****************************************************************/ /* Plot user-defined unary function f(x) = sinc between -6 and 6 */ x = plotdata(-6.0, 6.0); f(x) = sinc; /* sinc is defined at the bottom of this file. */ plot(x, f(x), "sinc(x)"); /*****************************************************************/ /* Plot user-defined binary function y = tanLessThan(x, max) */ /* Read tanLessThan code to see how values > max are not plotted */ x = plotdata(-270.0, 270.0); f2(x) = tanLessThan; /* Do not plot y values greater than 20 */ plot(x, f2(x, 20), "tan(x)", REDRED); /*********************************************************************/ /* Plot 2 functions on same graph (could be any number of functions) */ /* Define Y as two functions */ clear(x); /* Re-use old X, but change its range to -75..245 degrees */ x << plotdata(-80.0, 255.0); y = f2(x, 3); /* First function */ breakplot(x, y); /* Break the plot between functions */ /* You could also use x << NOPLOT; y << NOPLOT; instead.*/ /* Define second function (2cos(2x)) on same range "point-by-point" */ for(int i = -80; i <= 255; i++) { x << i; y << 2 * cos(2 * i * DEG_TO_RAD); } plot(x, y, "tan(x) and 2cos(2x)", BLUEBLUE); /*********************************************************** * You can also use the << operator to insert data at the end * of already entered plot data in order to draw 2 or more * functions. * * Use NOPLOT to separate different function data in both X * and Y, but be careful of the order of insertion! Each NOPLOT * in X must correspond to a NOPLOT in Y. * * Koolplot does not plot NOPLOT values. */ x = plotdata(-315.0, 45.0); y = sin(x * DEG_TO_RAD); plotdata z = cos(x * DEG_TO_RAD); plotdata p = sin(2 * (x - 45) * DEG_TO_RAD); plotdata q = cos(2 * x * DEG_TO_RAD); setColor(x, y, CRIMSON); // Will also break the plot x << plotdata(-315.0, 45.0); y << z; setColor(x, y, DARKORANGE ); x << plotdata(-315.0, 45.0); y << p; setColor(x, y, BLUEBLUE ); x << plotdata(-315.0, 45.0); y << q; // Default plot colour is green plot(x, y, COLOR(0,160,0), " sin(x) -green-, cos(x) -red-, sin(2x - 45) -orange-, cos(2x) -blue-"); /*************************************************/ return 0; }
void main() { int i; float dat1[SAMP_SIZE], fr1, j; char fname1[] = "fourtest1.txt"; char fname2[] = "fourtest2.txt"; char fname3[] = "fourtest3.txt"; FILE *file= NULL; printf("Frequency : "); scanf("%f", &fr1); file = fopen("fourtest1.txt","w"); if(file == NULL) { printf("Could not open the file"); // exit(0); } for(i=0;i<SAMP_SIZE;i++) { j=i; dat1[i] = sin(2*M_PI *i*fr1/SAMP_F); //printf("\n%d\t%f",i,dat1[i]); fprintf(file,"%f\t%f\n", (j/SAMP_F),dat1[i]); } plotdata(fname1); fclose(file); file = fopen("fourtest2.txt","w"); if(file == NULL) { printf("Could not open the file"); // exit(0); } // Fourier Transform of dat1 four1(dat1, SAMP_SIZE /2, 1); for(i=2;i<SAMP_SIZE/2;i++) { j=i; //printf("\n%d\t%f",i,dat1[i]); fprintf(file,"%f\t%f\n",(j-2)/4,dat1[i]); } plotdata(fname2); fclose(file); file = fopen("fourtest3.txt","w"); if(file == NULL) { printf("Could not open the file"); // exit(0); } // Inverse fourier transform of the dat1 four1(dat1, SAMP_SIZE /2, -1); for(i=0;i<SAMP_SIZE;i++) { j=i; //printf("\n%d\t%f",i,dat1[i]); fprintf(file,"%f\t%f\n",j/SAMP_F,dat1[i]/(SAMP_F)); } plotdata(fname3); fclose(file); }