⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generators.c

📁 dspic开发控制程序,有助开发pic单片机程序
💻 C
字号:
#include <p30f6014.h>
#include <stdlib.h>
#include <dsp.h>
#include "common.h"
#include "timers.h"
#include "generators.h"

typedef struct
{
	unsigned int PhzAcc;
	unsigned int PhzInc;
	unsigned int PhzOffset;
	fractional Scale;
	const int* SineTable;
}NCOStruct;

//用于产生sine 波形的数值表
const int SINE_TBL[512] = 
{
    0,   402,   804,  1206,  1607,  2009,  2410,  2811, 
 3211,  3611,  4011,  4409,  4807,  5205,  5601,  5997, 
 6392,  6786,  7179,  7571,  7961,  8351,  8739,  9126, 
 9511,  9895, 10278, 10659, 11038, 11416, 11792, 12166, 
12539, 12909, 13278, 13645, 14009, 14372, 14732, 15090, 
15446, 15799, 16150, 16499, 16845, 17189, 17530, 17868, 
18204, 18537, 18867, 19194, 19519, 19840, 20159, 20474, 
20787, 21096, 21402, 21705, 22004, 22301, 22594, 22883, 
23169, 23452, 23731, 24006, 24278, 24546, 24811, 25072, 
25329, 25582, 25831, 26077, 26318, 26556, 26789, 27019, 
27244, 27466, 27683, 27896, 28105, 28309, 28510, 28706, 
28897, 29085, 29268, 29446, 29621, 29790, 29955, 30116, 
30272, 30424, 30571, 30713, 30851, 30984, 31113, 31236, 
31356, 31470, 31580, 31684, 31785, 31880, 31970, 32056, 
32137, 32213, 32284, 32350, 32412, 32468, 32520, 32567, 
32609, 32646, 32678, 32705, 32727, 32744, 32757, 32764, 
32767, 32764, 32757, 32744, 32727, 32705, 32678, 32646, 
32609, 32567, 32520, 32468, 32412, 32350, 32284, 32213, 
32137, 32056, 31970, 31880, 31785, 31684, 31580, 31470, 
31356, 31236, 31113, 30984, 30851, 30713, 30571, 30424, 
30272, 30116, 29955, 29790, 29621, 29446, 29268, 29085, 
28897, 28706, 28510, 28309, 28105, 27896, 27683, 27466, 
27244, 27019, 26789, 26556, 26318, 26077, 25831, 25582, 
25329, 25072, 24811, 24546, 24278, 24006, 23731, 23452, 
23169, 22883, 22594, 22301, 22004, 21705, 21402, 21096, 
20787, 20474, 20159, 19840, 19519, 19194, 18867, 18537, 
18204, 17868, 17530, 17189, 16845, 16499, 16150, 15799, 
15446, 15090, 14732, 14372, 14009, 13645, 13278, 12909, 
12539, 12166, 11792, 11416, 11038, 10659, 10278,  9895, 
 9511,  9126,  8739,  8351,  7961,  7571,  7179,  6786, 
 6392,  5997,  5601,  5205,  4807,  4409,  4011,  3611, 
 3211,  2811,  2410,  2009,  1607,  1206,   804,   402, 
    0,  -402,  -804, -1206, -1607, -2009, -2410, -2811, 
-3211, -3611, -4011, -4409, -4807, -5205, -5601, -5997, 
-6392, -6786, -7179, -7571, -7961, -8351, -8739, -9126, 
-9511, -9895, -10278, -10659, -11038, -11416, -11792, -12166, 
-12539, -12909, -13278, -13645, -14009, -14372, -14732, -15090, 
-15446, -15799, -16150, -16499, -16845, -17189, -17530, -17868, 
-18204, -18537, -18867, -19194, -19519, -19840, -20159, -20474, 
-20787, -21096, -21402, -21705, -22004, -22301, -22594, -22883, 
-23169, -23452, -23731, -24006, -24278, -24546, -24811, -25072, 
-25329, -25582, -25831, -26077, -26318, -26556, -26789, -27019, 
-27244, -27466, -27683, -27896, -28105, -28309, -28510, -28706, 
-28897, -29085, -29268, -29446, -29621, -29790, -29955, -30116, 
-30272, -30424, -30571, -30713, -30851, -30984, -31113, -31236, 
-31356, -31470, -31580, -31684, -31785, -31880, -31970, -32056, 
-32137, -32213, -32284, -32350, -32412, -32468, -32520, -32567, 
-32609, -32646, -32678, -32705, -32727, -32744, -32757, -32764, 
-32767, -32764, -32757, -32744, -32727, -32705, -32678, -32646, 
-32609, -32567, -32520, -32468, -32412, -32350, -32284, -32213, 
-32137, -32056, -31970, -31880, -31785, -31684, -31580, -31470, 
-31356, -31236, -31113, -30984, -30851, -30713, -30571, -30424, 
-30272, -30116, -29955, -29790, -29621, -29446, -29268, -29085, 
-28897, -28706, -28510, -28309, -28105, -27896, -27683, -27466, 
-27244, -27019, -26789, -26556, -26318, -26077, -25831, -25582, 
-25329, -25072, -24811, -24546, -24278, -24006, -23731, -23452, 
-23169, -22883, -22594, -22301, -22004, -21705, -21402, -21096, 
-20787, -20474, -20159, -19840, -19519, -19194, -18867, -18537, 
-18204, -17868, -17530, -17189, -16845, -16499, -16150, -15799, 
-15446, -15090, -14732, -14372, -14009, -13645, -13278, -12909, 
-12539, -12166, -11792, -11416, -11038, -10659, -10278, -9895, 
-9511, -9126, -8739, -8351, -7961, -7571, -7179, -6786, 
-6392, -5997, -5601, -5205, -4807, -4409, -4011, -3611, 
-3211, -2811, -2410, -2009, -1607, -1206,  -804,  -402
};

//外部函数,其定义在AsmUtils.s中
extern void CalcNCO( unsigned int Size, fractional* pOut, NCOStruct* pNCO); 

static NCOStruct Nco;
static NCOStruct NcoI;
static NCOStruct NcoQ;

//函数原型
void InitGen(void);
void SetSineFreq( unsigned int freq);
void SetShiftFreq( int freq);
void MakeSine( unsigned int numsamples, int* pBuf, fractional Scale);
void MakeSinCos( unsigned int numsamples, int* pBufI,int* pBufQ, fractional Scale);
void MakeUniform( unsigned int numsamples, int* pBuf, fractional Scale);

//初始化发生器子程序
void InitGen(void)
{
	Nco.PhzAcc = 0;
	Nco.PhzOffset = 0;
	Nco.Scale = 0x7FFF;
	Nco.SineTable = SINE_TBL;
	SetSineFreq(1000);

	NcoI.PhzAcc = 0;
	NcoI.PhzOffset = 0;
	NcoI.Scale = 0x7FFF;
	NcoI.SineTable = SINE_TBL;
	NcoQ.PhzAcc = 0;
	NcoQ.PhzOffset = 0x4000;
	NcoQ.Scale = 0x7FFF;
	NcoQ.SineTable = SINE_TBL;
	SetShiftFreq( 500 );
}

//设定Sine波发生器频率子程序
void SetSineFreq( unsigned int freq)
{
	Nco.PhzInc = (int)( ( (unsigned long int)freq * 65536 )/SAMPLE_RATE);
}

//设定偏移频率子程序
void SetShiftFreq( int freq)
{
	NcoI.PhzInc = (int)( ( (long int)freq * 65536 )/SAMPLE_RATE);
	NcoQ.PhzInc = (int)( ( (long int)freq * 65536 )/SAMPLE_RATE);
}

//产生Sine波子程序
void MakeSine( unsigned int numsamples, int* pBuf, fractional Scale)
{
	Nco.Scale = Scale;
	CalcNCO( numsamples, pBuf, &Nco);
}

//建立Sine,Cosine波子程序
void MakeSinCos( unsigned int numsamples, int* pBufI,int* pBufQ, fractional Scale)
{
	NcoI.Scale = Scale;
	NcoQ.Scale = Scale;
	CalcNCO( numsamples, pBufI, &NcoI);
	CalcNCO( numsamples, pBufQ, &NcoQ);
}

//产生随机数子程序
void MakeUniform( unsigned int numsamples, int* pBuf, fractional Scale)
{
int i;
	for(i=0; i<numsamples; i++)
		pBuf[i] = (int)(rand()*2) - RAND_MAX;
	VectorScale(numsamples, pBuf, pBuf, Scale );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -