I have to write a C++ program to generate a square wave with amplitude from +1 to -1 and with following sampples:
65536
32768
16384
8128
4096
2048
1024
512
256
128
All these are 2 X times the prevoius one. ( 128 X2; 256 X 2; ... etc)
I need to get the data points of the squarewave with these different sample lengths to be saved as ASCII file.
Can any one help me ith this?
I tried to generate a square wave with amplitude that can be entered by the user. How can i save the data points with these different sample lengths in ASCII format??
Also i need to see the output. I am doing this as win32 application in C++
Thanks
--------------------------------------------#include <math.h>
#include <stdio.h>
sorry for any misunderstandings. I am quite new to C++. I have created a Win32 application with this one as my cpp file.
BUt I am not getting any output. I need to save the generated data as an ASCII file.
Ok. i ll give it a try whether it works. But i have got another problem now.
The result looks like this now:
216.0000000 1.0000000
220.0000000 1.0000000
224.0000000 -1.0000000
228.0000000 1.0000000
232.0000000 1.0000000
236.0000000 -1.0000000
240.0000000 1.0000000
244.0000000 1.0000000
248.0000000 -1.0000000
252.0000000 1.0000000
256.0000000 1.0000000
260.0000000 -1.0000000
264.0000000 1.0000000
-------------------------------------------------------------
1) I want only the left column to be saved in the file
2) and the format should not be with decimal. ( should be like 260, 264, etc) what should i do to get the correct format.
What should i change in "%12.7f\t%12.7f\n". Can anyone give me an explanation or link?
thnx galik.
I would like to add the number of points also as input.
-----------------------------
int main(int argc, char* argv[])
{
POINT points[200];
int n;
-------------------------------
here it is a constant value 200. I would like the user to enter this value. To do this I tried to include the following commands.
------------------------------
int i,n;
for(i = 0; i<n;i++)
{
POINT points[i];
}
-----------------------
My intention is to enter the number of points "n" as input . But this does not work. Is there any other possibility?=
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
struct POINT { int x,y; };
int main(int argc, char* argv[])
{
int no_of_points = 128; // default value
if(argc > 1) // did the user supply an argument?
{
// if so read the argument into no_of_points.
std::istringstream(argv[1]) >> no_of_points;
}
// declare your array
POINT* points1 = new POINT[no_of_points]; // standard
// ... stuff ...
delete[] points1; // remember to delete
// A much better method, is to use std::vector
std::vector<POINT> points(no_of_points);
points[10].x = 9;
std::cout << points[10].x << std::endl;
}
Then when you run the program you supply the number of samples
Still am getting some errors which i cannot find the problem.
Errors:
1)error C2470: "POINT": looks like a function definition but it is no parameter list available; visible function body will be skipped.
2) error C2447: ' {': function header missing - parameter list in the old style?}
#include "stdafx.h"
#include <iostream>
//#include <math.h> // this is for C
#include <cmath> // Use this for C++
// #include <stdio.h> // this is for C
#include <cstdio> // Use this for C++
#include <fstream>
#include <sstream>
#include <vector>
usingnamespace std;
double p ,a;
double square(constdouble x)
{
// Period and amplitude.
staticconstdouble period = p;
staticconstdouble amplitude = a;
// Force positive argument.
constint neg = x < 0.0;
constdouble xx = neg ? -x : x;
// Scale the argument and compute the return value.
constdouble x_scaled = xx - floor(xx / period) * period;
constdouble ret_val = x_scaled < period / 2.0 ? amplitude : -amplitude;
// Antisymmetric square wave.
return !neg ? ret_val : -ret_val;
}
// no need for typedef struct in C++
struct POINT
{
double x;
double y;
};
int main(int argc, char* argv[]) //; <= no semicolon here
// int n; ?? why is this here?
{//ERROR 2
unsignedint n; // needs to be inside the function
cout << "argc = " << argc << endl;
int no_of_points = 128; // default value
if(argc > 1)
std::istringstream(argv[1]) >> no_of_points;
POINT* points = new POINT[no_of_points];
// ... stuff ...
cout << "Enter the Period :\n";
cin >> p;
cout << "Enter the Amplitude:\n";
cin >> a;
constdouble increment = 0.5;
points[0].x = 0;
points[0].y = square(points[0].x);
for(n = 1; n < no_of_points; n++)
{
points[n].x = points[n - 1].x + increment;
points[n].y = square(points[n].x);
}
// Don't delete these BEFORE you have used them
// delete[] points1; // remember to delete
//Create a file to write to
FILE *OutFile = fopen("Extern_stimulussignal.sig","w");
for(n = 0; n < no_of_points; n++)
{
printf("%12.7f\t%12.7f\n", points[n].x, points[n].y);
//Send data to file
fprintf(OutFile,"%12.7f\n", points[n].y);
//("%12.7f\t%12.7f\n", points[n].x, points[n].y);
}
// only delete these AFTER you have finished with them
delete[] points;
//Close the file
fclose(OutFile);
cout << endl;
cin.get();
return 0;
}
thanks galik... As a beginner I hv many problems both in theoretical and practical side. now i need 2 focus on more on theory. Thanks for you input and help. I will try to do the rest my own.. learning by doin :P..atleast giving it a try..
hallo Galik
I need to modify the program like this.
Suppose i enter 128 samples. I need to do the program for
128*(512/1)
128*(512/2)
128*(512/4)
128*(512/8)
128*(512/16)
.
.
128*(512/512)
i.e. 128 *(512/(2^n))
And i need all data points one after the other in the same file.
I modified the program by adding a for loop. Is this correct? but when i run the program i get a stack flow error.
#include "stdafx.h"
#include <iostream>
//#include <math.h> // this is for C
#include <cmath> // Use this for C++
// #include <stdio.h> // this is for C
#include <cstdio> // Use this for C++
#include <fstream>
#include <sstream>
#include <vector>
usingnamespace std;
double p ,a;
double square(constdouble x)
{
// Period and amplitude.
staticconstdouble period = (p/2);
staticconstdouble amplitude = a;
// Force positive argument.
constint neg = x < 0.0;
constdouble xx = neg ? -x : x;
// Scale the argument and compute the return value.
constdouble x_scaled = xx - floor(xx / period) * period;
constdouble ret_val = x_scaled < period / 2.0 ? amplitude : -amplitude;
// Antisymmetric square wave.
return !neg ? ret_val : -ret_val;
}
// no need for typedef struct in C++
struct POINT
{
double x;
double y;
};
int main(int argc, char* argv[]) //; <= no semicolon here
// int n; ?? why is this here?
{//ERROR 2
unsignedint n; // needs to be inside the function
cout << "Number of Samples = " << endl;
//cout << "argc = " << argc << endl;
int no_of_points = 128; // default value
cin >> no_of_points;
if(argc > 1)
std::istringstream(argv[1]) >> no_of_points;
POINT* points = new POINT[no_of_points];
// ... stuff ...
cout << "Enter the Period :\n";
cin >> p;
cout << "Enter the Amplitude:\n";
cin >> a;
int w ;
int g= 512;
for(w=1;w<10;w++)
{
constdouble increment = 0.5;
points[0].x = 0;
points[0].y = square(points[0].x);
for(n = 1; n < (no_of_points*g)/2^w; n++)
{
points[n].x = points[n - 1].x + increment;
points[n].y = square(points[n].x);
}
}
// Don't delete these BEFORE you have used them
// delete[] points1; // remember to delete
//Create a file to write to
FILE *OutFile = fopen("Extern_stimulussignal.sig","w");
for(n = 0; n < (no_of_points*g)/2^w; n++)
{
printf("%12.7f\t%12.7f\n", points[n].x, points[n].y);
//Send data to file
fprintf(OutFile,"%12.7f\n", points[n].y);
//("%12.7f\t%12.7f\n", points[n].x, points[n].y);
}
// only delete these AFTER you have finished with them
delete[] points;
//Close the file
fclose(OutFile);
cout << endl;
cin.get();
return 0;
}
The problem is that you are asking for the number of points, then you are trying to create 512 times more points than you asked for, after only having allocated the number asked. You need to allocate enough points to begin with here:
POINT* points = new POINT[no_of_points]; // How many points are needed altogether?
OK i allocated 130944 points. because that much is the number of points i need at the end. But i am getting only 65472 points. y is that?
it should be like :
128*512/1 = 65536
128*512/2 =32768
128*512/4= 16384
.....
128*512/512=128
On adding all these we get 130944 points. i.e for the first loop in FOR loop it should save 65536 data points and then 32768 in the second loop below the first result and then 16384 below it in the same file ......and so on
Do i need to save the data each time or will it save all the points in buffer?
I think the problem is that you complete all the loops before saving the data. Inside the loops you are overwriting the previous data because n starts from the beginning again.
I think you need to open the file, start the loops and write each array full of points as you go. In fact, I don't see why you need to store the data in an array at all. You might as well just write each point to the file as you calculate it inside the loop.
thnx galik. i opened the file before the loop and entered them each time. But the total number of points i needed at end is 130944. But after this loop function am getting only 130934. i.e 10 less. is that due to the for loop for (w<10). how come that happens?? 10 less??
,----------------------------------------
oops sorry galik. Now i found the problem. That was due to the comparator sign. It works now.