Squarewave signal with different sample lengths

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>

double square(const double x)
{
static const double period = 4.0;
static const double amplitude = 2.0;

const int neg = x < 0.0;
const double xx = neg ? -x : x;
const double x_scaled = xx - floor(xx / period) * period;
const double ret_val = x_scaled < period / 2.0 ? amplitude : -amplitude;

return !neg ? ret_val : -ret_val;
}

typedef struct
{
double x;
double y;
}
POINT;

int main(int argc, char* argv[])
{
POINT points[200];
int n;

const double increment = 0.05;
points[0].x = -5.0;
points[0].y = square(points[0].x);

for(n = 1; n < sizeof(points) / sizeof(points[0]); n++)
{
points[n].x = points[n - 1].x + increment;
points[n].y = square(points[n].x);
}

for(n = 0; n < sizeof(points) / sizeof(points[0]); n++)
{
printf("%12.7f\t%12.7f\n", points[n].x, points[n].y);
}

return 0;
}

Last edited on
What is it about your program that is not working? It appears to do what you want.
Last edited on
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.
Then you need to open a file and print to the file instead of the console.

Something like:

1
2
3
4
5
6
7
8
9
10
11
FILE* fp = fopen("output.txt", "w");

// ... stuff

for(n = 0; n < sizeof(points) / sizeof(points[0]); n++)
{
    fprintf(fp, "%12.7f\t%12.7f\n", points[n].x, points[n].y);
}

close(fp);
Is it possibel to save these data points in ASCII format?. At the moment it is saving in decimal format in text file, right?
It is saving them in ASCII format. They are decimal numbers, but their ASCII representation is being stored in the 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?
You could try this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>

// ... stuff

std::ofstream fp("output.txt");  // use streams rather than old FILEs

// ... stuff

for(n = 0; n < sizeof(points) / sizeof(points[0]); n++)
{
    fp << static_cast<int>(points[n].x) << '\n';
}
Last edited on
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?=
How do you want the user to provide the input?

You could make the user provide the input as a parameter on the command line:

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
#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

> squaregen.exe 256

Last edited on
Ok.. i modified the whole program like this:
------------------------

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include<iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <vector>


using namespace std;
double p ,a;

double square(const double x)
	
{
	// Period and amplitude.
	static const double period = p;
	static const double amplitude = a;

	// Force positive argument.
	const int neg = x < 0.0;
	const double xx = neg ? -x : x;
	
	// Scale the argument and compute the return value.
	const double x_scaled = xx - floor(xx / period) * period;
	const double ret_val = x_scaled < period / 2.0 ? amplitude : -amplitude;

	// Antisymmetric square wave.
	return !neg ? ret_val : -ret_val;
}

typedef struct
{
	double x;
	double y;
}

POINT
{ // ERROR 1
	int x,y;
}

int main(int argc, char* argv[]);
	int n;
 {//ERROR 2
cout << "argc = " << argc << endl;


	int no_of_points = 128; // default value

	if(argc > 1) 

		
		std::istringstream(argv[1]) >> no_of_points;

	
	POINT* points1 = new POINT[no_of_points]; 



	// ... stuff ...

	cout << "Enter the Period :\n";
	cin >> p;
	cout << "Enter the Amplitude:\n";
	cin >> a;

	const double increment = 0.5;
	points1[0].x = 0;
	points1[0].y = square(points1[0].x);

	for(n = 1; n < sizeof(points1) / sizeof(points1[0]); n++)
	{
		points1[n].x = points1[n - 1].x + increment;
		points1[n].y = square(points1[n].x);
	}


	delete[] points1; // remember to delete

	
	//Create a file to write to
	FILE *OutFile = fopen("Extern_stimulussignal.sig","w");

	for(n = 0; n < sizeof(points1) / sizeof(points1[0]); n++)
	{

		printf("%12.7f\t%12.7f\n", points1[n].x, points1[n].y);
		
		//Send data to file
		fprintf(OutFile,"%12.7f\n", points1[n].y);
		//("%12.7f\t%12.7f\n", points[n].x, points[n].y);
	}

	//Close the file 
	fclose(OutFile);
	cout << endl;
	cin.get();
return 0;
}


------------------------------------------------------------------------

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?}

See in the program comment ERROR 1 & 2
Last edited on
Okay, I made some notes and corrections.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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>


using namespace std;
double p ,a;

double square(const double x)

{
	// Period and amplitude.
	static const double period = p;
	static const double amplitude = a;

	// Force positive argument.
	const int neg = x < 0.0;
	const double xx = neg ? -x : x;

	// Scale the argument and compute the return value.
	const double x_scaled = xx - floor(xx / period) * period;
	const double 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

	unsigned int 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;

	const double 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.

Underlined part is modified one.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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>


using namespace std;
double p ,a;

double square(const double x)

{
	// Period and amplitude.
	static const double period = (p/2);
	static const double amplitude = a;

	// Force positive argument.
	const int neg = x < 0.0;
	const double xx = neg ? -x : x;

	// Scale the argument and compute the return value.
	const double x_scaled = xx - floor(xx / period) * period;
	const double 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

	unsigned int 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++)
		{
			const double 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;
}



Last edited on
This may be the problem:

 
for(n = 1; n < (no_of_points*g)/2^w; n++)


In C++ 2^w does NOT mean 2 to the power of w!

You probably want this:


 
for(n = 1; n < (no_of_points*g)/pow(2, w); n++)

No error are shown . but when i run the program it stop showing this error

Unhandled exception occurred 0x012a1df3 in testprgm.exe: 0xc0000005: access violation when writing where 0x001b6000.

any idea??
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?
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "stdafx.h"
#include <iostream>
#include <cmath> 
#include <cstdio> 
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

double p ,a;

double square(const double x)

{
	// Period and amplitude.
	static const double period = (p/2);
	static const double amplitude = a;

	// Force positive argument.
	const int neg = x < 0.0;
	const double xx = neg ? -x : x;

	// Scale the argument and compute the return value.
	const double x_scaled = xx - floor(xx / period) * period;
	const double ret_val = x_scaled < period / 2.0 ? amplitude : -amplitude;

	// Antisymmetric square wave.
	return !neg ? ret_val : -ret_val;
}

struct POINT
{
	double x;
	double y;
};

int main(int argc, char* argv[]) //; <= no semicolon here
 {
	unsigned int n; 

	cout << "Number of Samples = " <<  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];

		
		cout << "Enter the Period :\n";

		cin >> p;

		cout << "Enter the Amplitude:\n";
		cin >> a;

		int w ;

		double g= 512;

		for(w=0;w<10;w++)
			{
				const double increment = 0.5;
				points[0].x = 0;
				points[0].y = square(points[0].x);

				for(n = 1; n < (128*g)/pow((double)2, w); n++)
					{
						points[n].x = points[n - 1].x + increment;
						points[n].y = square(points[n].x);
					}
			}
	
	
	//Create a file to write to
	FILE *OutFile = fopen("Extern_stimulussignal.sig","w");

	for(n = 0; n < (no_of_points*g)/pow((double)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);
	}

	delete[] points;

	//Close the file
	fclose(OutFile);

	cout << endl;

	cin.get();

return 0;
}


Last edited on
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.

Thankss!!!!
Last edited on
Topic archived. No new replies allowed.