# generating waves

Discussion in 'C Programming' started by pillip, Feb 6, 2004.

1. ### pillipGuest

I am trying to generate a number of waves. For square wave i am using a mod
funcion in a loop:
(i & 2)* amplitude , and running a circular buffer. For a triangular and saw
tooth waves I am all out of ideas. Now i know a saw tooth function is
already implemented in C, but i was wondering if it could be produced
without it.
Thanks in advance

pillip, Feb 6, 2004

2. ### MacGuest

On Fri, 06 Feb 2004 03:14:48 +0000, pillip wrote:

> I am trying to generate a number of waves. For square wave i am using a mod
> funcion in a loop:

Generating waves requires special hardware that is not on-topic in this
newsgroup. Of course, it sounds as though you really want to create a
numerical representation of a wave. This could potentially be on topic
here provided there is some C code involved.

> (i & 2)* amplitude , and running a circular buffer. For a triangular and saw
> tooth waves I am all out of ideas. Now i know a saw tooth function is
> already implemented in C,

Huh? What are you talking about? Implemented where, and by whom? Surely it
is not part of standard C?

> but i was wondering if it could be produced
> without it.

"It produced without it?" To what does "it" refer? Do you mean could a saw
tooth function be produced without c? If so, this is definitely the wrong
group. Do you mean could a sawtooth function be produced in c "from
scratch?" If so, I'm sure it can. What have you tried? People don't
usually just write your program for you. They expect you to try first, and
then they'll help you.

Can you write an equation for one iteration of a sawtooth in a
mathematical notation?

> Thanks in advance

Good luck.

--Mac

Mac, Feb 6, 2004

3. ### pillipGuest

Obviously you havent heard of:
value = fmod(currentTime, wavePeriod
which generates a saw tooth for the desired period

"Mac" <> wrote in message
news...
> On Fri, 06 Feb 2004 03:14:48 +0000, pillip wrote:
>
> > I am trying to generate a number of waves. For square wave i am using a

mod
> > funcion in a loop:

>
> Generating waves requires special hardware that is not on-topic in this
> newsgroup. Of course, it sounds as though you really want to create a
> numerical representation of a wave. This could potentially be on topic
> here provided there is some C code involved.
>
> > (i & 2)* amplitude , and running a circular buffer. For a triangular and

saw
> > tooth waves I am all out of ideas. Now i know a saw tooth function is
> > already implemented in C,

>
> Huh? What are you talking about? Implemented where, and by whom? Surely it
> is not part of standard C?
>
> > but i was wondering if it could be produced
> > without it.

>
> "It produced without it?" To what does "it" refer? Do you mean could a saw
> tooth function be produced without c? If so, this is definitely the wrong
> group. Do you mean could a sawtooth function be produced in c "from
> scratch?" If so, I'm sure it can. What have you tried? People don't
> usually just write your program for you. They expect you to try first, and
> then they'll help you.
>
> Can you write an equation for one iteration of a sawtooth in a
> mathematical notation?
>
> > Thanks in advance

>
> Good luck.
>
> --Mac
>

pillip, Feb 6, 2004
4. ### CBFalconerGuest

Mac wrote:
> On Fri, 06 Feb 2004 03:14:48 +0000, pillip wrote:
>
> > I am trying to generate a number of waves. For square wave i
> > am using a mod funcion in a loop:

>
> Generating waves requires special hardware that is not on-topic in
> this newsgroup. Of course, it sounds as though you really want to
> create a numerical representation of a wave. This could potentially
> be on topic here provided there is some C code involved.
>
> > (i & 2)* amplitude , and running a circular buffer. For a
> > triangular and saw tooth waves I am all out of ideas. Now i know
> > a saw tooth function is already implemented in C,

>
> Huh? What are you talking about? Implemented where, and by whom?
> Surely it is not part of standard C?
>
> > but i was wondering if it could be produced without it.

>
> "It produced without it?" To what does "it" refer? Do you mean could
> a saw tooth function be produced without c? ... snip ...

Methinks the OP has already produced wave generators in c.l.c.
Of complaints, of revulsion, of trivial commentary, etc.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

CBFalconer, Feb 6, 2004
5. ### Simon BiberGuest

"pillip" <> wrote:
> I am trying to generate a number of waves. For square wave i am using
> a mod funcion in a loop: (i & 2)* amplitude , and running a circular
> buffer. For a triangular and saw tooth waves I am all out of ideas.
> Now i know a saw tooth function is already implemented in C, but i
> was wondering if it could be produced without it.

Here are some functions and ASCII art graphs of them.

y = fmod(x, 1)

0 1 2 3 4 5 6
1 | /| /| /| /| /| /|
| / | / | / | / | / | / |
| / | / | / | / | / | / |
| / | / | / | / | / | / |
0 |/____|/____|/____|/____|/____|/____|________ x
|
|
|
|
-1 |

y = fabs(2 - fmod(x, 4.0)) - 1

0 1 2 3 4 5 6 7 8
1 |\ /\ /
| \ / \ /
| \ / \ /
| \ / \ /
0 |____\__________/________\__________/______ x
| \ / \ /
| \ / \ /
| \ / \ /
| \ / \ /
-1 | \/ \/

y = 2 * round(fmod(x, 1)) - 1

0 1 2 3 4 5 6 7
1 | _____ _____ _____
| | | | | | |
| | | | | | |
| | | | | | |
0 |_____|_____|_____|_____|_____|_____|_______ x
| | | | | | |
| | | | | | |
| | | | | | |
-1 |_____| |_____| |_____| |_____

--
Simon.

Simon Biber, Feb 6, 2004
6. ### Mark A. OdellGuest

"pillip" <> wrote in
news::

> Obviously you havent heard of:
> value = fmod(currentTime, wavePeriod
> which generates a saw tooth for the desired period

Which standard C library is that in? What header file should I include?
What? It's not in the standard C library? Oh, then it is off-topic here.

--
- Mark ->
--

Mark A. Odell, Feb 6, 2004
7. ### Mark A. OdellGuest

"Mark A. Odell" <> wrote in
news:Xns94878977A3EB7CopyrightMarkOdell@130.133.1.4:

>> Obviously you havent heard of:
>> value = fmod(currentTime, wavePeriod
>> which generates a saw tooth for the desired period

>
> Which standard C library is that in? What header file should I include?
> What? It's not in the standard C library? Oh, then it is off-topic here.

Eh, heh. I'm just finishing up some crow over here. Obviously, I don't use
the math lib on 8-bit micros that much. My apologies.

--
- Mark ->
--

Mark A. Odell, Feb 6, 2004
8. ### Christopher Benson-ManicaGuest

Mark A. Odell <> spoke thus:

> Eh, heh. I'm just finishing up some crow over here. Obviously, I don't use
> the math lib on 8-bit micros that much. My apologies.

Don't feel too bad - I had a similar post written and one :wq away
from posting, but something said "Hm, better check K&R" first...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Christopher Benson-Manica, Feb 6, 2004
9. ### MacGuest

On Fri, 06 Feb 2004 14:45:13 +0000, pillip wrote:

[original post altered to fix top-posting]

> "Mac" <> wrote in message
> news...
>> On Fri, 06 Feb 2004 03:14:48 +0000, pillip wrote:
>>
>> > I am trying to generate a number of waves. For square wave i am using a

> mod
>> > funcion in a loop:

>>
>> Generating waves requires special hardware that is not on-topic in this
>> newsgroup. Of course, it sounds as though you really want to create a
>> numerical representation of a wave. This could potentially be on topic
>> here provided there is some C code involved.
>>
>> > (i & 2)* amplitude , and running a circular buffer. For a triangular and

> saw
>> > tooth waves I am all out of ideas. Now i know a saw tooth function is
>> > already implemented in C,

>>
>> Huh? What are you talking about? Implemented where, and by whom? Surely it
>> is not part of standard C?
>>
>> > but i was wondering if it could be produced
>> > without it.

>>
>> "It produced without it?" To what does "it" refer? Do you mean could a saw
>> tooth function be produced without c? If so, this is definitely the wrong
>> group. Do you mean could a sawtooth function be produced in c "from
>> scratch?" If so, I'm sure it can. What have you tried? People don't
>> usually just write your program for you. They expect you to try first, and
>> then they'll help you.
>>
>> Can you write an equation for one iteration of a sawtooth in a
>> mathematical notation?
>>
>> > Thanks in advance

>>
>> Good luck.
>>
>> --Mac
>>

[top-posting relocated here]

> Obviously you havent heard of:
> value = fmod(currentTime, wavePeriod
> which generates a saw tooth for the desired period
>
>
>

In what sense can a single value be considered a sawtooth wave? Anyway, it
seems that you clearly have the skill and resourcefulness to generate your
own sawtooth.

Here's one approach:

#include <stdio.h>
#include <stdlib.h>

struct waveform
{ int size;
double *data;
};

void destroy_waveform(struct waveform *w)
{ if (w != NULL)
free(w->data);
free(w);
}

struct waveform *sawtooth(double amplitude, int period)
{ struct waveform *w;
double delta;
int i;

if (period <= 1)
return NULL;
w = malloc(sizeof *w);
if (w == NULL)
return NULL;
w->data = malloc(sizeof *w->data * period);
if (w->data == NULL)
{ free(w);
return NULL;
}
delta = amplitude / (period - 1);
for(i = 0; i < period; i++)
w->data = delta * i;
w->size = period;
return w;
}

int main(void)
{ int i;
struct waveform *w;
w = sawtooth(10, 10);
if (w == NULL)
{ puts("Error, out of memory.\n");
return EXIT_FAILURE;
}
for (i = 0; i < w->size; i++)
printf("data[%d] = %f\n", i, w->data);
return EXIT_SUCCESS;
}

regards,
Mac

Mac, Feb 7, 2004

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.