Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C++ help: Simple square wave.

Status
Not open for further replies.

fireflies

Newbie level 5
Newbie level 5
Joined
Feb 1, 2006
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
square wave c

Hi,

I am new to C++/Cprogramming
How do you generate a simple square wave using C++.

void Square(void)
{
float Amplitude_S, Frequency_S, S, Sq, t, sgn_z, z;

clrscr();

textbackground( fBLUE + fINTENSITY );

gotoxy( 5, 1 );
textbackground( fWHITE + fINTENSITY );
printf("Enter Amplitude: ");
fflush(stdin);
scanf("%f", &Amplitude_S);
textbackground( fBLUE + fINTENSITY );
gotoxy( 5, 2 );
printf("Frequency: ");
scanf("%f", &Frequency_S);
t = 1/Frequency_S;
z = sin(t);
sgn_z = z / abs(z);
S = sgn_z; //take initial phase as 0
//sgn = sign or signum function
Sq = Amplitude_S * S;

textbackground( fWHITE + fINTENSITY );
printf("Input Complete...");
fflush(stdin);
getch();
}//end

Thanks.
 

c++ square wave

This is a bizarre little program.
First, you're asking for the Amplitude and the the Frequency which is ok.
Then you do some calculations which don't make any sense.
And then you don't do anything with the calculations.

So I'll have to make some sweeping assumptions in order to help you.
I assume that you're trying to draw a square wave or trying to generate data that represents a square wave at the Amplitude and Frequency that the user inputs.

As far as your calculations -
I think that you are using the sign of the sine to determine if the square wave is high (+) or low (-) which will work, but there is a better way (see below)

The equation you need is:
x = Amplitude * sin( 2 * pi * f * t)
f = Frequency from user
t = time of the sample = dt * n

It's not clear at all what you're trying to do with the square wave.
Let's just assume that you want to represent one second of time in the drawing with 100 points.

Then dt = delta time = 1 sec/100 = 0.01 sec
n = 0 to 99 (100 points)
f = Frequency from user (1 to 50 Hz only)

for( n=0; n<100; n++)
{
int P;
A = sin( 2 * PI * f * dt * n);
if( A >= 0 )
P = Amplitude;
else
P = -Amplitude;
draw(n, P); // draw a dot at x=n, y=P (plus screen offsets)
}

An easier way to do this without using any trig functions is the modulo operator
So instead of
A = sin( 2 * PI * f * dt * n);
Use
A = 1 - 2 * ((n/f) %2); // A will be either +1 or =1

The term sin(t) that you calculate doesn't have any meaning as far as I can tell.

My assumptions about what you're trying to do are probably wrong but maybe there is something in there that you can use.

Hope this helps.
 

square wave in c

hi!

generate square wave in what form??

for example

a) draw it
b) write it to a file
c) write it to a port

except in case c you can make/synthesise square waves
which is accurate in terms of frequency
where as in case c where you are generating in relatime
it matters, what frequency you are trying to generate ( feasibility)
& how accurate you want it to be

/*
generate square wave
================
let Fs be the sampling frequency & f be the square wave frequency
then one cycle of square wave will take Fs/f cycles.

so there will be Fs/2f one time & Fs/2f off time */

N=Fs/2*f;out=0;//initialise

while (1){
if(( n++)%N==0)out-=1; // toggle at half cycle of square wave

// use the 'out' variable to
// generate the square wave

};
 

c program to generate square wave

This is better math than my way which didn't need to be so complicated.
Just a couple of minor things -

1. Need to init n
int n=0;

2. out calc is wrong
if(( n++)%N==0)out-=1; // this will just grow -1,-2,-3,-4,...
should be:
if(( n++)%N==0)out*=-1; // this will toggle, -1,+1,-1,+1...

3. Putting multiple statements all on one line is really bad form, it should be:
if( (n++) % N == 0 )
out *= -1;

4. Reliance on order of presedence is dangerous; compilers are free to evaluate from left to right or right to left for equal precedence operators - except for Boolean operators.

N=Fs/2*f;
should be
N = Fs / (2 * f);
which is different than
N = (Fs / 2 ) * f;
without parentheses, it's not guaranteed what you'll get.
And you're helping everyone who reads your code to understand what it's doing.
 

square wave c++

Hi, thanks guys. I think I get the hang of it, just need to read more C++ books I guess.

Hmm, as for firefox's method, is there something I can do to control the amplitude?
 

square wave program in c

Just initialize "out".
If you start it at 10 then you'll get +10,-10,+10,-10, etc
 

generate square wave c++

it is best if u use graphics. i used to do that before... printing waves.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top