generating waves

P

pillip

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
 
M

Mac

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
 
P

pillip

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

CBFalconer

Mac said:
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.


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


"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.
 
S

Simon Biber

pillip said:
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 |_____| |_____| |_____| |_____
 
M

Mark A. Odell

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.
 
M

Mark A. Odell

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.
 
C

Christopher Benson-Manica

Mark A. Odell said:
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... ;)
 
M

Mac

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

[original post altered to fix top-posting]

[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top