DSPRelated.com
Forums

sine wave generation

Started by decorr April 11, 2006
can any one please help me in creatind a sine wave in C/C++ please
i also need to play it as a .wav file any random sound within sudible
frequency will do

decorr wrote:
> can any one please help me in creatind a sine wave in C/C++ please > i also need to play it as a .wav file any random sound within sudible > frequency will do
??? Do you need a sine wave on a wav file, or do you need an arbitrary sound that is audible? You'd have to be more specific in describing what you need if you want us to give you a hand. Carlos --
decorr wrote:
> can any one please help me in creatind a sine wave in C/C++ please > i also need to play it as a .wav file any random sound within sudible > frequency will do
#define N 1024 short sound[N]; int k; for(k = 0;k < N/4;k++) { sound[4*k] = 0x7FFF; sound[4*k+1] = 0; sound[4*k+2] = 0x8000; sound[4*k+3] = 0; }
john wrote:
> decorr wrote: > >>can any one please help me in creatind a sine wave in C/C++ please >>i also need to play it as a .wav file any random sound within sudible >>frequency will do > > > #define N 1024 > > short sound[N]; > int k; > > for(k = 0;k < N/4;k++) { > sound[4*k] = 0x7FFF; > sound[4*k+1] = 0; > sound[4*k+2] = 0x8000; > sound[4*k+3] = 0; > }
Dear god, that's going to hurt like hell! ;-) Carlos --
Carlos Moreno wrote:
> john wrote: > >> decorr wrote: >> >>> can any one please help me in creatind a sine wave in C/C++ please >>> i also need to play it as a .wav file any random sound within sudible >>> frequency will do >> >> >> >> #define N 1024 >> >> short sound[N]; >> int k; >> >> for(k = 0;k < N/4;k++) { >> sound[4*k] = 0x7FFF; >> sound[4*k+1] = 0; >> sound[4*k+2] = 0x8000; >> sound[4*k+3] = 0; >> } > > > Dear god, that's going to hurt like hell! ;-) > > Carlos
I wouldn't hear it. How about we drop the frequency and raise the amplitude a bit? for(k = 0;k < N/6;k++) { sound[4*k] = 0x7FFF; sound[4*k+1] = 0x7FFF; sound[4*k+2] = 0; sound[4*k+3] = -0x7FFF; sound[4*k+4] = -0x7FFF; sound[4*k+5] = 0; } Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
"john" <johns@xetron.com> writes:

> decorr wrote: >> can any one please help me in creatind a sine wave in C/C++ please >> i also need to play it as a .wav file any random sound within sudible >> frequency will do > > #define N 1024 > > short sound[N]; > int k; > > for(k = 0;k < N/4;k++) { > sound[4*k] = 0x7FFF; > sound[4*k+1] = 0; > sound[4*k+2] = 0x8000; > sound[4*k+3] = 0; > }
1. Not a sine. Try 0x8001 instead of 0x8000. ;) 2. Not audible for Fs > ~80 kHz. :o -- % Randy Yates % "...the answer lies within your soul %% Fuquay-Varina, NC % 'cause no one knows which side %%% 919-577-9882 % the coin will fall." %%%% <yates@ieee.org> % 'Big Wheels', *Out of the Blue*, ELO http://home.earthlink.net/~yatescr
>>> decorr wrote: >>> >>>> can any one please help me in creatind a sine wave in C/C++ please >>>> i also need to play it as a .wav file any random sound within
sudible
>>>> frequency will do
Are you looking for writing a C/C++ program that produces "sound" of a sine wave? Or are you looking for a C/C++ program to have an appropriate sine function output to be used somewhere else? If you want the sound, I think the below one is very audible.
> >for(k = 0;k < N/6;k++) { > sound[4*k] = 0x7FFF; > sound[4*k+1] = 0x7FFF; > sound[4*k+2] = 0; > sound[4*k+3] = -0x7FFF; > sound[4*k+4] = -0x7FFF; > sound[4*k+5] = 0; >} > >Jerry
If you want the sine wave approximation, then I would suggest you go through "The Art of Computer Programming - Volume-2 (Seminumerical Algorithm)" by Donald Knuth. Excellent Taylor series based approximation programs are there in it. - Krishna
Randy Yates wrote:
> "john" <johns@xetron.com> writes: > > > decorr wrote: > >> can any one please help me in creatind a sine wave in C/C++ please > >> i also need to play it as a .wav file any random sound within sudible > >> frequency will do > > > > #define N 1024 > > > > short sound[N]; > > int k; > > > > for(k = 0;k < N/4;k++) { > > sound[4*k] = 0x7FFF; > > sound[4*k+1] = 0; > > sound[4*k+2] = 0x8000; > > sound[4*k+3] = 0; > > } > > 1. Not a sine. Try 0x8001 instead of 0x8000. ;)
I'll bet you would not be able to tell me which is which in a listening test.
> > 2. Not audible for Fs > ~80 kHz. :o > --
Last time I checked audio sample rates were < 80 kHz. John
"john" <johns@xetron.com> writes:

> Randy Yates wrote: >> "john" <johns@xetron.com> writes: >> >> > decorr wrote: >> >> can any one please help me in creatind a sine wave in C/C++ please >> >> i also need to play it as a .wav file any random sound within sudible >> >> frequency will do >> > >> > #define N 1024 >> > >> > short sound[N]; >> > int k; >> > >> > for(k = 0;k < N/4;k++) { >> > sound[4*k] = 0x7FFF; >> > sound[4*k+1] = 0; >> > sound[4*k+2] = 0x8000; >> > sound[4*k+3] = 0; >> > } >> >> 1. Not a sine. Try 0x8001 instead of 0x8000. ;) > > I'll bet you would not be able to tell me which is which in a listening > test.
Yeah, but what about my dog...
>> 2. Not audible for Fs > ~80 kHz. :o >> -- > > Last time I checked audio sample rates were < 80 kHz.
Then you haven't checked lately! DVD-Audio supports sample rates of 96 and 192 kHz! Many sound cards these days are supporting extended sample rates past the traditional 32, 44.1, and 48 kHz. Don't take me wrong - it was all in fun... -- % Randy Yates % "And all that I can do %% Fuquay-Varina, NC % is say I'm sorry, %%% 919-577-9882 % that's the way it goes..." %%%% <yates@ieee.org> % Getting To The Point', *Balance of Power*, ELO http://home.earthlink.net/~yatescr
Randy Yates wrote:
> "john" <johns@xetron.com> writes: > > > Randy Yates wrote: > >> "john" <johns@xetron.com> writes: > >> > >> > decorr wrote: > >> >> can any one please help me in creatind a sine wave in C/C++ please > >> >> i also need to play it as a .wav file any random sound within sudible > >> >> frequency will do > >> > > >> > #define N 1024 > >> > > >> > short sound[N]; > >> > int k; > >> > > >> > for(k = 0;k < N/4;k++) { > >> > sound[4*k] = 0x7FFF; > >> > sound[4*k+1] = 0; > >> > sound[4*k+2] = 0x8000; > >> > sound[4*k+3] = 0; > >> > } > >> > >> 1. Not a sine. Try 0x8001 instead of 0x8000. ;) > > > > I'll bet you would not be able to tell me which is which in a listening > > test. > > Yeah, but what about my dog... > > >> 2. Not audible for Fs > ~80 kHz. :o > >> -- > > > > Last time I checked audio sample rates were < 80 kHz. > > Then you haven't checked lately! DVD-Audio supports sample rates of 96 > and 192 kHz! Many sound cards these days are supporting extended > sample rates past the traditional 32, 44.1, and 48 kHz. > > Don't take me wrong - it was all in fun... > --
Yes, I knew about those higher rates. I think a dog would even have a hard time hearing Fs/4 on some of those. It does bring up the possiblity of using the soundcard for low-cost signal generation and capture. I have been working with some students at a midwestern university and they are using high Fs audio cards for DSP comms work such as modulation, demodulation, etc. John