![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 8
| generate sinusoidal wave using hash table |
| sme7000 is offline | |
| | #2 |
| Registered User Join Date: Dec 2006 Location: Canada
Posts: 2,322
| Using the sin() function perhaps? |
| cyberfish is offline | |
| | #3 |
| Staff software engineer 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |