Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generating waves

Reply
Thread Tools

generating waves

 
 
pillip
Guest
Posts: n/a
 
      02-06-2004
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


 
Reply With Quote
 
 
 
 
Mac
Guest
Posts: n/a
 
      02-06-2004
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

 
Reply With Quote
 
 
 
 
pillip
Guest
Posts: n/a
 
      02-06-2004
Obviously you havent heard of:
value = fmod(currentTime, wavePeriod
which generates a saw tooth for the desired period



"Mac" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-06-2004
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      02-06-2004
"pillip" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      02-06-2004
"pillip" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) s.com:

> 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 ->
--
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      02-06-2004
"Mark A. Odell" <(E-Mail Removed)> 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 ->
--
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-06-2004
Mark A. Odell <(E-Mail Removed)> 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.
 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      02-07-2004
On Fri, 06 Feb 2004 14:45:13 +0000, pillip wrote:

[original post altered to fix top-posting]

> "Mac" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> 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[i] = 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[i]);
return EXIT_SUCCESS;
}

regards,
Mac

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating sin/square waves sound Paulo da Silva Python 9 01-02-2012 06:09 PM
Making Waves timrobinson@paradise.net.nz Java 12 11-21-2005 08:11 AM
mixing sampled sine waves viswanath VHDL 1 05-12-2004 06:02 AM
Waves/pointer phrogee Computer Support 1 10-03-2003 02:07 PM
Whats the cause of the waves on my TFT monitor? Chris Computer Support 2 09-21-2003 07:08 PM



Advertisments