C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-18-2009, 01:15 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 8
generate sinusoidal wave using hash table

how can i generate sinusoidal wave using hash table, or look up table?
sme7000 is offline   Reply With Quote
Old 12-18-2009, 01:17 AM   #2
Registered User
 
Join Date: Dec 2006
Location: Canada
Posts: 2,322
Using the sin() function perhaps?
cyberfish is offline   Reply With Quote
Old 12-18-2009, 01:25 AM   #3
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
I don't think you'd ever use a hash table to generate a sine wave. A lookup table is more likely. Generally you would precompute a single cycle of a sine wave into a table of some fixed size, then draw values from the table as needed, maybe with interpolation.

If it's not homework I'll show code.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-18-2009, 02:49 AM   #4
Registered User
 
Join Date: Oct 2009
Posts: 8
after determine the point of this half cycle of sinusoidal wave, i choose to use 10 point per half cycle. this point are store in a look-up table, how can i use this point in next step..( such as comparing with other wave). is it any example to call this function and create lookup table?
sme7000 is offline   Reply With Quote
Old 12-18-2009, 04:03 AM   #5
Registered User
 
Join Date: Sep 2006
Posts: 3,720
Hard to give advice on a wave that I know very little about:

1) how tall do you want it to be?

2) how wide?

3) how many waves do you want to fit on one screen?

You can get a nice sine wave by taking your maxy setting for your window and setting:
ypos = maxy / 2;

then your crest can be:

crest = maxy / 4;

then using a width of maxx, put that into a for loop, with i as the counter,

and use: swing=crest*sin(10*pi*i/width);
with: putpixel(i, ypos+swing);

and work that from 0 to max x.

That doesn't use a lookup table or a hash, but it works nicely - even on a 486 @ 50 MHz, lol.
Adak is offline   Reply With Quote
Old 12-18-2009, 10:05 AM   #6
Registered User
 
Join Date: Nov 2009
Posts: 82
sme7000, This is how you generate, and use a lookup table for trig functions, in pseudo.

*note; I've given an example of the most basic way to use a lookup table, in practice it can be much more complicated and requires a lot of numerical 'tricks' to get indices 'right'.

Depending on the application I wouldn't even bother with lookup tables. Sometimes though you need to, maybe on highly limited hardware or something... don't bother with this unless you actually have to. Just IMO.

Code:
#define T_RESOLUTION 360
T_SIN[T_RESOLUTION] = { ... };
T_COS[T_RESOLUTION] = { ... };
// Trig tables contain a range of values, [0, 2pi], with given resolution.

// generate ...
for (int i = 0; i < T_RESOLUTION ; ++i)
{
	T_SIN[i] = sin(i * 2.0 * M_PI / (float)T_RESOLUTION);
	T_COS[i] = cos(i * 2.0 * M_PI / (float)T_RESOLUTION);
}

// draw a circle with lookup tables  ...
for (int i = 0; i < T_RESOLUTION ; ++i)
{
	px = T_COS[i] * Radius; // no trig calculations! yay lookup tables!
	py = T_SIN[i] * Radius;
	draw(px, py);
}

Last edited by since; 12-18-2009 at 10:12 AM.
since is offline   Reply With Quote
Old 12-18-2009, 01:17 PM   #7
Registered User
 
Join Date: Oct 2009
Posts: 8
thanks for this advice and knowledge...i try first and see the result
sme7000 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dictionary in C# to hash table in C? dinoman C Programming 2 04-12-2009 09:23 PM
Writing array, to file zootreeves C Programming 9 09-08-2007 05:06 PM
Group Project Help/Volunteer DarkDot C++ Programming 3 04-24-2007 11:36 PM
Hash table creation and insertion tgshah C Programming 1 01-23-2006 07:54 PM
Not sure on hash table memory allocations Thumper333 C Programming 3 09-27-2004 09:00 PM


All times are GMT -6. The time now is 12:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22