|
本帖最后由 yjmwxwx 于 2017-1-19 16:48 编辑
以前没玩过生成正弦波,我数学小学5年级水平看不懂公式,更不会使用 Math。自己算不了只能找现成的了,于是放狗搜索找到一个在
线的,是外国一个制作设备的公司提供的,矿坛不准发连接于是每法发,比应搜索 Sine Look Up Table Generator Calculator 就能搜索
到。
后来又找了一个,不过是命令行的没有界面,他这个还有源码。 科班的别笑我,我不会写也不会算只能找别人的。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- int main(int argc,char *argv[])
- {
- int entries, entries_2PI, phase, amplitude, offset, data_per_line;
- int i, j;
- int max, min, max_digits;
- if (argc != 7) {
- fprintf(stderr,"Generate C source code for sine table\n");
- fprintf(stderr,"Usage:\n");
- fprintf(stderr,"sinus_gen entries entries_per_2PI phase amplitude offset data_per_line >outputfile\n\n");
- fprintf(stderr,"entries : number of entries in sine table\n");
- fprintf(stderr,"entries_per_2PI : number of samples within full 360 degree\n");
- fprintf(stderr,"phase : phase offset in samples\n");
- fprintf(stderr,"amplitude : amplitude of sine wave, integer\n");
- fprintf(stderr,"offset : offset of sine wave, integer\n");
- fprintf(stderr,"data_per_line : number of samples per line in source file\n");
- fprintf(stderr,">outputfile : redirect output form terminal to file\n");
- return 1;
- }
- entries = atoi(argv[1]);
- entries_2PI = atoi(argv[2]);
- phase = atoi(argv[3]);
- amplitude = atoi(argv[4]);
- offset = atoi(argv[5]);
- data_per_line = atoi(argv[6]);
- min = abs(offset - amplitude);
- max = abs(offset + amplitude);
- max_digits=2;
- if (min > 99 || max > 99) max_digits=3;
- if (min > 999 || max > 999) max_digits=4;
- if (min > 9999 || max > 9999) max_digits=5;
- // header
- printf("const int sinus[%d] = {\n\t", entries);
- for (i=0, j=0; i<entries; i++) {
- switch(max_digits) {
- case 2: printf("%3d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- case 3: printf("%4d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- case 4: printf("%5d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- case 5: printf("%6d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- }
- if (i < (entries-1)) {
- printf(", ");
- }
- j++;
- if (j==data_per_line) {
- if (i < (entries-1)) {
- printf("\n\t");
- } else {
- printf("\n");
- }
- j=0;
- }
- }
- if (j !=0) printf("\n");
- // footer
- printf("};\n");
- return 0;
- }
复制代码
可执行程序后面跟着7个参数不知道是什么,我用在线翻译也不懂是什么意思,求学过C语言的解释。我想问的就是这个问题。
不过经过我摸索比如我想生成 180个点的表。
第1个和第2个参数都填 180、
第3个相偏移是0、
第4个第5个是单片机定时器里放的数除2, 比如定时器里放40这里就填20
第6个是表有多少列
./a.out 180 180 0 20 20 8
WINDOWS版不知道为什么运行不起来,我的是精简XP,自己查下有没有病毒,我没装杀毒
Sinus_gen.zip
(45.71 KB, 下载次数: 104)
放到单片机里面实验一下, 我用的是STM32F030F4P6,便宜2元多。 我的这个片子8M晶振,PLL 14 倍频,工作还算稳定
下面这个程序是用了1024个点的, 定时器里放1024,DMA数据1024个。 不会计算频率, 瞎算一个 112000000 / 1024 / 1024 = 106.xxx HZ
RC滤波电路是 100欧和104电容,在线算的15000HZ截止频率,我不懂是什么意思。
PA10和PB1两路互补输出。
自己写的单片机小程序,不会C语言只能汇编了,网上也找不到参考的代码,只能瞎写了。。
STM32F030F4P6正弦波.zip
(8.13 KB, 下载次数: 95)
查表不是办法,还得让单片机自己算。 第二个问题是 CORDIC算法什么原理? 网上讲的一片公式,对于我这小学数学的实在看不懂。
老外网站上找了个可以算正弦的已经可以算出来了,听说这个算法很快? 别笑我哦,只是瞎搞水平很低,现在学什么都学不进去。
我只会到地里打个药锄个草什么的。
CORDIC.zip
(5.66 KB, 下载次数: 91)
|
评分
-
3
查看全部评分
-
|