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! "); 22 else 23 { 24 printf("File created successfully! "); 25 /* 26 * 生成文件头:注意不要忘了“;” 27 */ 28 fprintf (fp, "DEPTH = %d; ",DEPTH); 29 fprintf (fp, "WIDTH = %d; ",WIDTH); 30 fprintf (fp, "ADDRESS_RADIX = HEX; "); 31 fprintf (fp, "DATA_RADIX = HEX; "); 32 fprintf (fp, "CONTENT "); 33 fprintf (fp, "BEGIN "); 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 : %x; ", i, temp); 48 }//end for 49 50 fprintf (fp, "END; "); 51 fclose (fp); 52 } 53 }
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! "); 21 else 22 { 23 printf("File created successfully! "); 24 /* 25 * 生成文件头:注意不要忘了“;” 26 */ 27 fprintf (fp, "DEPTH = %d; ",DEPTH); 28 fprintf (fp, "WIDTH = %d; ",WIDTH); 29 fprintf (fp, "ADDRESS_RADIX = HEX; "); 30 fprintf (fp, "DATA_RADIX = HEX; "); 31 fprintf (fp, "CONTENT "); 32 fprintf (fp, "BEGIN "); 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 : %x; ", i, temp); 46 }//end for 47 48 fprintf (fp, "END; "); 49 fclose (fp); 50 } 51 }
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! "); 21 else 22 { 23 printf("File created successfully! "); 24 /* 25 * 生成文件头:注意不要忘了“;” 26 */ 27 fprintf (fp, "DEPTH = %d; ",DEPTH); 28 fprintf (fp, "WIDTH = %d; ",WIDTH); 29 fprintf (fp, "ADDRESS_RADIX = HEX; "); 30 fprintf (fp, "DATA_RADIX = HEX; "); 31 fprintf (fp, "CONTENT "); 32 fprintf (fp, "BEGIN "); 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 : %x; ", i, temp); 41 }//end for 42 43 fprintf (fp, "END; "); 44 fclose (fp); 45 } 46 }
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! "); 20 else 21 { 22 printf("File created successfully! "); 23 /* 24 * 生成文件头:注意不要忘了“;” 25 */ 26 fprintf (fp, "DEPTH = %d; ",DEPTH); 27 fprintf (fp, "WIDTH = %d; ",WIDTH); 28 fprintf (fp, "ADDRESS_RADIX = HEX; "); 29 fprintf (fp, "DATA_RADIX = HEX; "); 30 fprintf (fp, "CONTENT "); 31 fprintf (fp, "BEGIN "); 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 : %x; ", i, temp); 43 }//end for 44 45 fprintf (fp, "END; "); 46 fclose (fp); 47 } 48 }
整合到一个函数里
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! "); 29 else 30 { 31 printf("File created successfully! "); 32 /* 33 * 生成文件头:注意不要忘了“;” 34 */ 35 fprintf (fp, "DEPTH = %d; ",DEPTH); 36 fprintf (fp, "WIDTH = %d; ",WIDTH); 37 fprintf (fp, "ADDRESS_RADIX = HEX; "); 38 fprintf (fp, "DATA_RADIX = HEX; "); 39 fprintf (fp, "CONTENT "); 40 fprintf (fp, "BEGIN "); 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 : %x; ", i, temp); 82 }//end for 83 84 fprintf (fp, "END; "); 85 fclose (fp); 86 } 87 }
如有错误还请指出,如有侵权还请告知,如需转载请注明出处!