码迷,mamicode.com
首页 > 编程语言 > 详细

mif文件C语言生成

时间:2017-12-30 23:39:19      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:color   create   blank   int   技术   for   linu   tar   c语言   

1:正弦波

用函数 sin (x * π/180°) 

技术分享图片
 1 /**************************************************
 2 
 3       正弦波 mif 生成
 4 
 5 ***************************************************/
 6 #include <stdio.h>
 7 #include <math.h>
 8 
 9 #define DEPTH 128     /*数据深度,即存储单元的个数,可更改*/
10 #define WIDTH 8       /*存储单元的宽度,可更改*/
11 #define PI 3.141592
12 
13 int main(void)
14 {
15     int i,temp;
16     float s;
17 
18     FILE *fp;
19     fp = fopen("SinPro.mif","w");   /*可更改,但扩展名必须为.mif*/
20     if(NULL == fp)
21         printf("Can not creat file!\r\n");
22     else
23     {
24         printf("File created successfully!\n");
25         /*
26         *    生成文件头:注意不要忘了“;”
27         */
28         fprintf (fp, "DEPTH = %d;\n",DEPTH);
29         fprintf (fp, "WIDTH = %d;\n",WIDTH);
30         fprintf (fp, "ADDRESS_RADIX = HEX;\n");
31         fprintf (fp, "DATA_RADIX = HEX;\n");
32         fprintf (fp, "CONTENT\n");
33         fprintf (fp, "BEGIN\n");
34 
35         /*
36         * 以十六进制输出地址和数据
37         */
38         for(i = 0; i < DEPTH; i++)
39         {
40              /*周期为128个点的正弦波*/ 
41             s = sin( PI * i * 2 / DEPTH);   // sin (x * π/180°) 取值范围-1至1,注意 sin() 函数是 double 类型
42 
43             /*将-1~1之间的正弦波的值扩展到0-255之间*/ 
44             temp = (int)( (s+1) * pow(2, WIDTH - 1) );  //正弦值扩展到 0 至 2 的 WIDTH次幂,注意 pow() 函数是 double 类型
45 
46             /*以十六进制输出地址和数据*/
47             fprintf (fp, "%x \t : \t %x;\n", i, temp);
48         }//end for
49         
50         fprintf (fp, "END;\n");
51         fclose (fp);
52     }
53 }
sin_pro.c

 

2:三角波

用斜率计算

技术分享图片
 1 /**************************************************
 2 
 3       三角波 mif 生成
 4 
 5 ***************************************************/
 6 #include <stdio.h>
 7 #include <math.h>
 8 
 9 #define DEPTH 128     /*数据深度,即存储单元的个数,可更改*/
10 #define WIDTH 8       /*存储单元的宽度,可更改*/
11 
12 int main(void)
13 {
14     int i,temp = 0;
15 
16 
17     FILE *fp;
18     fp = fopen("TrianglePro.mif","w");   /*可更改,但扩展名必须为.mif*/
19     if(NULL == fp)
20         printf("Can not creat file!\r\n");
21     else
22     {
23         printf("File created successfully!\n");
24         /*
25         *    生成文件头:注意不要忘了“;”
26         */
27         fprintf (fp, "DEPTH = %d;\n",DEPTH);
28         fprintf (fp, "WIDTH = %d;\n",WIDTH);
29         fprintf (fp, "ADDRESS_RADIX = HEX;\n");
30         fprintf (fp, "DATA_RADIX = HEX;\n");
31         fprintf (fp, "CONTENT\n");
32         fprintf (fp, "BEGIN\n");
33 
34         /*
35         * 以十六进制输出地址和数据
36         */
37         for(i = 0; i < DEPTH; i++)
38         {
39             if (i <= DEPTH / 2)
40                 //temp = (int)(i * (pow (2, WIDTH) - 1) * 2 / DEPTH);
41                 temp = (int)(((pow (2, WIDTH) - 1) * 2 / DEPTH) * i);  //斜率 * i
42             else
43                 //temp = (int)(temp - (pow (2, WIDTH) - 1) * 2 / DEPTH);
44                 temp = (int)(((pow (2, WIDTH) - 1) * 2 / DEPTH) * (DEPTH - 1 - i));  //斜率 * (depth-1 - i)
45             fprintf (fp, "%x \t : \t %x;\n", i, temp);
46         }//end for
47         
48         fprintf (fp, "END;\n");
49         fclose (fp);
50     }
51 }
triangle_pro.c

 

3:锯齿波

用斜率计算

技术分享图片
 1 /**************************************************
 2 
 3       锯齿波 mif 生成
 4 
 5 ***************************************************/
 6 #include <stdio.h>
 7 #include <math.h>
 8 
 9 #define DEPTH 128     /*数据深度,即存储单元的个数,可更改*/
10 #define WIDTH 8       /*存储单元的宽度,可更改*/
11 
12 int main(void)
13 {
14     int i,temp = 0;
15 
16 
17     FILE *fp;
18     fp = fopen("SawtoothPro.mif","w");   /*可更改,但扩展名必须为.mif*/
19     if(NULL == fp)
20         printf("Can not creat file!\r\n");
21     else
22     {
23         printf("File created successfully!\n");
24         /*
25         *    生成文件头:注意不要忘了“;”
26         */
27         fprintf (fp, "DEPTH = %d;\n",DEPTH);
28         fprintf (fp, "WIDTH = %d;\n",WIDTH);
29         fprintf (fp, "ADDRESS_RADIX = HEX;\n");
30         fprintf (fp, "DATA_RADIX = HEX;\n");
31         fprintf (fp, "CONTENT\n");
32         fprintf (fp, "BEGIN\n");
33 
34         /*
35         * 以十六进制输出地址和数据
36         */
37         for(i = 0; i < DEPTH; i++)
38         {    
39             temp = (int)(((pow (2, WIDTH) - 1) / (DEPTH - 1)) * i);  //斜率 * i
40             fprintf (fp, "%x \t : \t %x;\n", i, temp);
41         }//end for
42         
43         fprintf (fp, "END;\n");
44         fclose (fp);
45     }
46 }
sawtooth_pro.c

 

4:方波

技术分享图片
 1 /**************************************************
 2 
 3       方波 mif 生成
 4 
 5 ***************************************************/
 6 #include <stdio.h>
 7 #include <math.h>
 8 
 9 #define DEPTH 128     /*数据深度,即存储单元的个数,可更改*/
10 #define WIDTH 8       /*存储单元的宽度,可更改*/
11 
12 int main(void)
13 {
14     int i,temp;
15     
16     FILE *fp;
17     fp = fopen("SquarePro.mif","w");   /*可更改,但扩展名必须为.mif*/
18     if(NULL == fp)
19         printf("Can not creat file!\r\n");
20     else
21     {
22         printf("File created successfully!\n");
23         /*
24         *    生成文件头:注意不要忘了“;”
25         */
26         fprintf (fp, "DEPTH = %d;\n",DEPTH);
27         fprintf (fp, "WIDTH = %d;\n",WIDTH);
28         fprintf (fp, "ADDRESS_RADIX = HEX;\n");
29         fprintf (fp, "DATA_RADIX = HEX;\n");
30         fprintf (fp, "CONTENT\n");
31         fprintf (fp, "BEGIN\n");
32 
33         /*
34         * 以十六进制输出地址和数据
35         */
36         for(i = 0; i < DEPTH; i++)
37         {
38             if (i < DEPTH / 2)
39                 temp = 0;
40             else
41                 temp = (int)(pow (2, WIDTH) - 1);
42             fprintf (fp, "%x \t : \t %x;\n", i, temp);
43         }//end for
44         
45         fprintf (fp, "END;\n");
46         fclose (fp);
47     }
48 }
square_pro.c

 

整合到一个函数里

 1  /**************************************************
 2 
 3        mif 文件生成
 4 
 5 ***************************************************/
 6 
 7 #include <stdio.h>
 8 #include <math.h>
 9 
10 /*-------------------------参数区------------------------*/
11 
12 #define DEPTH 128     /* 数据深度,即存储单元的个数*/
13 #define WIDTH 8       /* 存储单元的宽度 */
14 #define MODE 4        /* 1:正弦波   2:三角波   3:锯齿波   4:方波 */
15 
16 /*-------------------------参数区------------------------*/
17 
18 
19 #define PI 3.141592
20 
21 int main(void)
22 {
23     int i,temp;
24 
25     FILE *fp;
26     fp = fopen("yl.mif","w");   /*可更改,但扩展名必须为.mif*/
27     if(NULL == fp)
28         printf("Can not creat file!\r\n");
29     else
30     {
31         printf("File created successfully!\n");
32         /*
33         *    生成文件头:注意不要忘了“;”
34         */
35         fprintf (fp, "DEPTH = %d;\n",DEPTH);
36         fprintf (fp, "WIDTH = %d;\n",WIDTH);
37         fprintf (fp, "ADDRESS_RADIX = HEX;\n");
38         fprintf (fp, "DATA_RADIX = HEX;\n");
39         fprintf (fp, "CONTENT\n");
40         fprintf (fp, "BEGIN\n");
41 
42         /*
43         * 以十六进制输出地址和数据
44         */
45 
46         for(i = 0; i < DEPTH; i++)
47         {
48             switch(MODE)
49             {
50                 case 1 ://sine
51                 {
52                     temp = (int)( (sin( PI * i * 2 / DEPTH) + 1) * pow(2, WIDTH - 1) );
53                     break;
54                 }
55                 case 2 ://triangle
56                 {
57                     if (i <= DEPTH / 2)
58                         temp = (int)(((pow (2, WIDTH) - 1) * 2 / DEPTH) * i);  //斜率 * i
59                     else
60                         temp = (int)(((pow (2, WIDTH) - 1) * 2 / DEPTH) * (DEPTH - 1 - i));
61                     break;
62                 }
63                 case 3 ://sawtooth
64                 {
65                     temp = (int)(((pow (2, WIDTH) - 1) / (DEPTH - 1)) * i);
66                     break;
67                 }
68                 case 4 ://square
69                 {
70                     if (i < DEPTH / 2)
71                         temp = 0;
72                     else
73                         temp = (int)(pow (2, WIDTH) - 1);
74                     break;
75                 }
76                 default:
77                 {
78                     break;
79                 }
80             }
81             fprintf (fp, "%x \t : \t %x;\n", i, temp);
82         }//end for
83 
84         fprintf (fp, "END;\n");
85         fclose (fp);
86     }
87 }

 

 

 

 

 

 

 

如有错误还请指出,如有侵权还请告知,如需转载请注明出处!                                              

本人博客:http://www.cnblogs.com/yllinux/

mif文件C语言生成

标签:color   create   blank   int   技术   for   linu   tar   c语言   

原文地址:https://www.cnblogs.com/yllinux/p/8151383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!