* Make the FFT size a bit larger.
[matthijs/projects/montium-fft.git] / FFT_support.cpp
index 23d07f08ca102e114e7428807701f364fcdcc566..2ddf150b6f1bb8e3e215dac89d9256cbb4fe31cd 100644 (file)
@@ -2,8 +2,7 @@
 #include <cstdio>\r
 #include <cmath>\r
 \r
-\r
-/* Didn't the Montium use Q15 instead of Q14? */\r
+/* Use Q15 fixed point format (1 sign bit plus 15 fractional bits) */\r
 #define FIXED_POINT 15\r
 #define WORD_SIZE   16\r
 \r
@@ -28,7 +27,7 @@ float from_fixed(int n)
        return n / (float)(1<<FIXED_POINT);\r
 }\r
 \r
-void print_mem(mem m, int offset, int size, bool fixed)\r
+void print_mem(mem m, int offset, int size, bool fixed, bool newline)\r
 {\r
        int i;\r
        for(i = offset;i<offset+size;i++)\r
@@ -37,12 +36,12 @@ void print_mem(mem m, int offset, int size, bool fixed)
                        printf("%0.4f", from_fixed(get_mem(m->id, i)));\r
                else\r
                        printf("%04hx", (short)get_mem(m->id, i));\r
-               if ((i + 1) % WORDS_PER_LINE == 0)\r
+               if (newline && (i + 1) % WORDS_PER_LINE == 0)\r
                        printf("\n");\r
                else if ((i + 1) % WORDS_PER_GROUP == 0)\r
                        printf(" ");\r
        }\r
-       if (i % WORDS_PER_LINE != 0)\r
+       if (newline && i % WORDS_PER_LINE != 0)\r
                printf("\n");\r
 }\r
 \r
@@ -68,7 +67,9 @@ void pre_run()
        \r
        for (i=0;i<PARAM_N_t;i++)\r
        {\r
-               int value = to_fixed(sin((float)i*2*M_PI/PARAM_N_t));\r
+               /* We take the sine from 0 to 20*2*Pi, ie twenty periods. We divide \r
+                * the value by PARAM_N_t to prevent overflow. */\r
+               int value = to_fixed(sin((float)i*20*2*M_PI/PARAM_N_t)/PARAM_N_t);\r
 \r
                if (i<PARAM_N_t/2)\r
                {\r
@@ -93,19 +94,32 @@ void pre_run()
        }\r
        \r
        printf("re(W)\n");\r
-       print_mem(twiddle_re, 0, PARAM_N_t/2, true);\r
+       print_mem(twiddle_re, 0, PARAM_N_t/2, true, true);\r
        printf("im(W)\n");\r
-       print_mem(twiddle_im, 0, PARAM_N_t/2, true);\r
+       print_mem(twiddle_im, 0, PARAM_N_t/2, true, true);\r
        printf("re(in_a)\n");\r
-       print_mem(input_a_re, 0, PARAM_N_t/2, true);\r
+       print_mem(input_a_re, 0, PARAM_N_t/2, true, true);\r
        printf("re(in_b)\n");\r
-       print_mem(input_b_re, 0, PARAM_N_t/2, true);\r
+       print_mem(input_b_re, 0, PARAM_N_t/2, true, true);\r
+\r
+       printf("re_in = [");\r
+       print_mem(input_a_re, 0, PARAM_N_t/2, true, false);\r
+       print_mem(input_b_re, 0, PARAM_N_t/2, true, false);\r
+       printf("];\n");\r
+\r
+/* Write out memory contents for use by the python simulator */\r
+       save_mem_range_to_file(input_a_re->id, 0, PARAM_N_t/2, "Memory/sin_a_re.mm");\r
+       save_mem_range_to_file(input_a_im->id, 0, PARAM_N_t/2, "Memory/sin_a_im.mm");\r
+       save_mem_range_to_file(input_b_re->id, 0, PARAM_N_t/2, "Memory/sin_b_re.mm");\r
+       save_mem_range_to_file(input_b_im->id, 0, PARAM_N_t/2, "Memory/sin_b_im.mm");\r
+       save_mem_range_to_file(twiddle_re->id, 0, PARAM_N_t/2, "Memory/twiddle_re.mm");\r
+       save_mem_range_to_file(twiddle_im->id, 0, PARAM_N_t/2, "Memory/twiddle_im.mm");\r
 }\r
 \r
 void post_run()\r
 {\r
        if (PARAM_n_t % 2 == 0) {\r
-               /* When the number of stages is odd, the \r
+               /* When the number of stages is even, the \r
                 * outputs end up at the left memories again */\r
                output_a_re  = alloc_mem(P0M0);\r
                output_a_im  = alloc_mem(P1M0);\r
@@ -117,11 +131,21 @@ void post_run()
                output_b_re  = alloc_mem(P2M1);\r
                output_b_im  = alloc_mem(P3M1);\r
        }\r
-       printf("re(out_a)\n");\r
-       print_mem(output_a_re, 0, PARAM_N_t/2, true);\r
-       print_mem(output_b_re, 0, PARAM_N_t/2, true);\r
-       printf("im(out_a)\n");\r
-       print_mem(output_a_im, 0, PARAM_N_t/2, true);\r
-       print_mem(output_b_im, 0, PARAM_N_t/2, true);\r
+       printf("re_out = [");\r
+       print_mem(output_a_re, 0, PARAM_N_t/2, true, false);\r
+       print_mem(output_b_re, 0, PARAM_N_t/2, true, false);\r
+       printf("];\n");\r
+       printf("im_out = [");\r
+       print_mem(output_a_im, 0, PARAM_N_t/2, true, false);\r
+       print_mem(output_b_im, 0, PARAM_N_t/2, true, false);\r
+       printf("];\n");\r
+       /*\r
+       printf("re(out)\n");\r
+       print_mem(output_a_re, 0, PARAM_N_t/2, false, true);\r
+       print_mem(output_b_re, 0, PARAM_N_t/2, false, true);\r
+       printf("im(out)\n");\r
+       print_mem(output_a_im, 0, PARAM_N_t/2, false, true);\r
+       print_mem(output_b_im, 0, PARAM_N_t/2, false, true);\r
+       */\r
 \r
 }\r