Rocksoccer's Fucking World

February 29, 2008

FFT — Fast Fourier Transform

Filed under: Computer & Programming — Rocksoccer @ 16:41

Some people might want this code. It is very useful in many signal processing
applications.

下载: fft.c
  1. /****************************************************************/
  2. /* fft.c */
  3. /* Douglas L. Jones */
  4. /* University of Illinois at Urbana-Champaign */
  5. /* January 19, 1992 */
  6. /* */
  7. /* fft: in-place radix-2 DIT DFT of a complex input */
  8. /* */
  9. /* input: */
  10. /* n: length of FFT: must be a power of two */
  11. /* m: n = 2**m */
  12. /* input/output */
  13. /* x: double array of length n with real part of data */
  14. /* y: double array of length n with imag part of data */
  15. /* x & y will store the data of the output*/
  16. /* but this this is array, so just pointer is transmitted, no need to return anything*/
  17. /* Permission to copy and use this program is granted */
  18. /* as long as this header is included. */
  19. /****************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <stdlib.h>
  24.  
  25. #include "wave.h"
  26.  
  27. void fft(int n, double *x, double *y)
  28. {
  29.     //int n,m;
  30.     //double x[],y[];
  31.  
  32.     int m = (int)(log(n)/log(2));
  33.  
  34.     int i,j,k,n1,n2;
  35.     double c,s,e,a,t1,t2;
  36.  
  37.     j = 0; /* bit-reverse */
  38.     n2 = n/2;
  39.     for (i=1; i < n - 1; i++)
  40.     {
  41.         n1 = n2;
  42.         while ( j >= n1 )
  43.         {
  44.             j = j - n1;
  45.             n1 = n1/2;
  46.         }
  47.         j = j + n1;
  48.         if (i < j)
  49.         {
  50.             t1 = x[i];
  51.             x[i] = x[j];
  52.             x[j] = t1;
  53.  
  54.             /* the input arrays are not initialized inside FFT()
  55.             therefore, an initialized array is needed.*/
  56.             t1 = y[i];
  57.             y[i] = y[j];
  58.             y[j] = t1;
  59.         }
  60.     }
  61.  
  62.     n1 = 0; /* FFT */
  63.     n2 = 1;
  64.     for (i=0; i < m; i++)
  65.     {
  66.         n1 = n2;
  67.         n2 = n2 + n2;
  68.         e = -6.283185307179586/n2;
  69.         a = 0.0;
  70.         for (j=0; j < n1; j++)
  71.         {
  72.             c = cos(a);
  73.             s = sin(a);
  74.             a = a + e;
  75.             for (k=j; k < n; k=k+n2)
  76.             {
  77.                 t1 = c*x[k+n1] - s*y[k+n1];
  78.                 t2 = s*x[k+n1] + c*y[k+n1];
  79.                 x[k+n1] = x[k] - t1;
  80.                 y[k+n1] = y[k] - t2;
  81.                 x[k] = x[k] + t1;
  82.                 y[k] = y[k] + t2;
  83.  
  84.                 /*printf("x %d = %e\n", k, x[k]);
  85.                 printf("y %d = %e\n", k, y[k]);*/
  86.             }
  87.  
  88.             //printf("Press Enter to continue");
  89.             //fflush(stdin);
  90.             //getchar();
  91.         }
  92.     }
  93.     return;
  94. }

Writing Code in your Posts

Filed under: Computer & Programming — Rocksoccer @ 16:00

Whether you write plugins or hacks for WordPress, or you want to add bits and pieces of code about your own WordPress site or other programming code like HTML, CSS, PHP, or javascripts, putting code in your post that “looks” like code, but “doesn’t behave” like code, is a frequent challenge for bloggers.

By default, WordPress will convert unrecognized uses of < and > into characters which actually look like &lt; and &gt;, which will “look” like a < and a > when posted. Or, if it finds the use of an HTML tag within the post, it will use the tag like it is HTML and you will have funky looking text and a messed up layout.

In general, there are two uses of code within a web page. There is code found within a paragraph to make a point about the code that is being discussed, and then there is code that is highlighted

in such a way as to look
like a box of code

Code Within Paragraphs

There are two HTML tags which will turn text into monospaced type. They are <code> and <tt>. The latter is rarely used today, replaced by the more useful and semantically correct <code>, which distinguishes text that is computer code from normal language.

This is an example of code used within
a paragraph to talk about the <code>index.php</code>,
<code>sidebar.php</code>, and <code>header.php</code>
template files in WordPress.

This is great for using the tag around words that you want to look like code, but what about tags like HTML that you want to showcase?

In the <code>header.php</code> template file,
look for the <code><div class="header"></code>
section to change the <code><h1></code> heading.

Using the <code> tag doesn’t tell WordPress to strip the HTML references from the post. It sees the <code> tag and then it sees the div and so it responds by creating a new container in your web page. WordPress thinks that you are actually using HTML tags, and the start of an h1 tag will screw up your entire web page layout and design.

In the header.php template file, look for the

section to change the heading.

To make WordPress recognize this as code within a paragraph, use character entities or extended characters to represent the left and right arrows and surround them with the <code> tags.

In the <code>header.php</code> template file,
look for the <code>&lt;div class="header"&gt;</code>
section to change the <code>&lt;h1&gt;</code> heading.

By default, WordPress will turn any phrase in the text that begins with http: into a link. If you are giving an example of how to link to a specific post within a WordPress site, instead of using the link with http://example.com/index.php?p=453 and having it turn into a link, you can use extended characters for the slashes, so WordPress won’t “see” the link.

...link to a specific WordPress post using
<code>http:&#47;&#47;example.com&#47;index.php?p=453</code>
in your post....

Here is a list of the most common HTML character entities:

< = &lt;
> = &gt;
/ = &#47;
] = &#93;
[ = &#91;
" = &#34;
' = &#39;

There is a list of resources below which will help you turn HTML tags into character entities automatically, so you don’t have to memorize these character codes.

Using PRE

To set your code aside so that it looks like a box of code which may be copied and pasted within other code or template file, you can use the <pre> HTML tag.

The <pre> tag instructs the browser to use the monospace code font, but to exactly reproduce whatever is inside of the <pre> tags. Every space, line break, every bit of code is exactly reproduced.

<h3>Section Three Title</h3>
<p>This is the start of a
<a title="article on relationships" href="goodtalk.php">
good relationship</a> between you and I....

Using the <pre> tag isn’t very “pretty” but it does the job. Examples of how to style it can be found in the next section. Still, it showcases the code exactly.

By exactly, we mean EXACTLY. If you have a long line of code, it will run off the page because there are no instructions which tell the code to wrap. It won’t. Here is an example:

  1. &lt;h3&gt;Section Three Title&lt;/h3&gt;&lt;p&gt;This is the start of a &lt;a title="article on relationships" href="goodtalk.php"&gt;good relationship&lt;/a&gt; between you and I and I think you should read it because it is important that we have these little &lt;a title="article on communication" href="communication.php"&gt;conversations&lt;/a&gt; once in a while to let each other know how we feel....

Not pretty, is it. To avoid this screen run-off, put in line breaks. Unfortunately, deciding where to put those line breaks in when you are showcasing code that will be copied makes it a difficult decision.

If you are familiar with programming language, you will know when a line break won’t mess up a line of code, so choose there. If you are unfamiliar with where to put line breaks, experiment. Put the code in, make the line breaks, and then test the code. If it works, then use the line break there. If not, change the line break position.

If you have long lines of code, consider showcasing only excerpts and providing a link to the full code placed on your site in a text or PHPS file, or using one of the many online pastebins which are available for temporarily showcasing code.

Troubleshooting Codes

Writing code within a WordPress post can be a challenge, overriding WordPress’ default styles and filters which “fix” what we write. If you are having trouble with writing code within your WordPress post, these might help.

Quotes in Codes

A frequent problem using codes within your post is WordPress’ technique of “styling” quote marks. Instead of seeing quotes, by default WordPress texturizes the quotes into open and closed quotes, like the quotes used in many word processing programs. Code that is to be copied needs to have the straight quotes, not the pretty quotes.

You can avoid this problem by using the <pre> tag or by actually including the character code for the quotes in the usage:

<p class="red">

becomes

<code>&lt;p class=&#34;red&#34;&gt;</code>

Unfortunately, if you edit a page after first publishing it, the html code editor replaces all these entities with their literal equivalents, so, for example, if you carefully use &#34; for your quotes, they’ll come back as ” and so when you save, they’ll be converted.

Styling Your Code Tags

By default, using <pre> and <code> will put the text in a monospaced font and use the font-size from the body tag. What if you want the font-size to be different, and the look of these tags to also have a different color or style to them?

In your style.css style sheet in your WordPress Theme folder, add styles to these two tags. Here is an example:

pre {border: solid 1px blue;
	font-size: 1.3 em;
 	color: blue;
	margin: 10px;
	padding:10px;
	background: #FFFFB3}
code {font-size:1.2em;
	color: #008099}

Using the <code> would look like that and the pre would look like this:

  1. pre {border: solid 1px blue;
  2.        font-size: 1.3 em;
  3.        color: blue;
  4.        margin: 10px;
  5.        padding:10px;
  6.        background: #FFFFB3}
  7.        code {font-size:1.2em;
  8.        color: #008099}

Is it better to use malloc or calloc to allocate memory?

Filed under: Computer & Programming — Rocksoccer @ 05:16

From: http://wiki.answers.com/Q/Is_it_better_to_use_malloc_or_calloc_to_allocate_memory

In general using malloc is faster, since calloc initializes the allocated memory to contain all zeroes. If this is what you want, however, then calloc can be used. The results can vary among different operating systems and environments, though.

Memory allocation in an OS that uses floating blocks in heaps, such as Microsoft Windows and MacOS, should use the OS-native memory allocators instead.

“Use malloc() almost always and calloc() almost never.”

The reason is that the initialization to zero that calloc() performs is usually not very helpful:

- The initialization to “all-bits-zero” is not necessarily the same as initialization to “all-data-zero.” C says very little about the representation of values in memory, nothing at all for floating-point or pointer values. On many machines all-bits-zero representations will in fact correspond to f.p. zeroes or null pointers, but this is not guaranteed by the language and there have been machines where the correspondence did not hold. If you get in the habit of using calloc() to initialize f.p. and pointer items, you may be heading for trouble.

- Usually, one allocates a chunk of dynamic memory in order to store something in it — and when you store something in it, you’ll overwrite whatever was there before. Thus, the initialization performed by calloc() is usually not needed anyhow. There are occasional exceptions where all- bits-zero initialization is helpful, but they are unusual.

Comments:

The most important point is the red words. I think most of C teachers would not emphasize this point. If you know this, you can evulate everything by yourself.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

What is the difference between malloc() and calloc()?

From: http://www.coders2020.com/what-is-the-difference-between-malloc-and-calloc

First lets look at the prototypes of these two popular functions..

  #include

  void *calloc(size_t n, size_t size);

  void *malloc(size_t size);

The two functions malloc() and calloc() are functionally same in that they both allocate memory from a storage pool (generally called heap). Actually, the right thing to say is that these two functions are memory managers and not memory allocators. Memory allocation is done by OS specific routines (like brk() and sbrk()). But lets not get into that for now…

Here are some differences between these two functions..

    * malloc() takes one argument, whereas calloc() takes two.
    * calloc() initializes all the bits in the allocated space to zero (this is all-bits-zero!,
       where as malloc() does not do this.
    * A call to calloc() is equivalent to a call to malloc() followed by one to memset().

    calloc(m, n)
    is essentially equivalent to
    p = malloc(m * n);
    memset(p, 0, m * n);

Using calloc(), we can carry out the functionality in a faster way than a combination of malloc() and memset() probably would. You will agree that one libray call is faster than two calls. Additionally, if provided by the native CPU, calloc() could be implementated by the CPU’s “allocate-and-initialize-to-zero” instruction.

* The reason for providing the “n” argument is that sometimes it is required to allocate a number (”n”) of uniform objects of a particular size (”size”). Database application, for instance, will have such requirements. Proper planning for the values of “n” and “size” can lead to good memory utilization.

Powered by WordPress