使用C語言產生正弦波數據

要生成正弦波,主要就是經過sin()函數用角度計算出每一個點的值就行,下面看一下C代碼的實現算法

#define PointMax 64
#define PI 3.1415926
unsigned int sinData[PointMax] = {0};

//point 一個週期內採樣的點數
//生成一個週期正弦波 水平線爲32  最大值爲64  最小值爲0 
void get_sin_tab( unsigned int point )
{
    unsigned int i = 0, j = 0;
    float hd = 0.0;       //弧度
    float fz = 0.0;         //峯值
    unsigned int tem = 0;
    j = point / 2;
    hd = PI / j;
    for( i = 0; i < point; i++ )
    {
        fz = j * sin( hd * i ) + j;
        tem = ( unsigned int )fz;
        sinData[i] = tem;
        printf("%d,",tem);
    }
    printf("\r\n");
}
int main()
{
	get_sin_tab(64);
	system("pause");
	return 0;
}
複製代碼

打印的數據以下:markdown

32,35,38,41,44,47,49,52,54,56,58,60,61,62,63,63,64,63,63,62,
61,60,58,56,54,52,49,47,44,41,38,35,32,28,25,22,19,16,14,11,
9,7,5,3,2,1,0,0,0,0,0,1,2,3,5,7,9,11,14,16,19,22,25,28函數

生成的正弦波圖形以下:spa

這種方法將點數的個數做爲最大值,好比生成64個點,那麼最大值就是64,若是生成128個點,最大值就是128.code

下面再看一種能夠設置點數和最大值的方法orm

int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
        fz = k * sin( hd * i ) + k;					//i=0時sin(0)=0 起始點爲最大值一半,也就是起始點是水平線位置  弧度值爲π/2內每一個點對應的弧度值
       

        tem = ( unsigned int )(fz * 1.0);						//經過係數能夠調整輸出有效值大小
        sinData[i] = tem;
        printf("%d\r\n",tem);
    }
}
int main()
{	
	get_sin_tab1(64,800);
	system("pause");
	return 0;
}
複製代碼

生成點以下:get

400,439,478,516,553,588,622,653,682,709,732,752,769,782,792,798,800,798,792,782,
769,752,732,709,682,653,622,588,553,516,478,439,400,360,321,283,246,211,177,146,
117,90,67,47,30,17,7,1,0,1,7,17,30,47,67,90,117,146,177,211,246,283,321,360it

生成圖形以下io

生成64個點,最大值爲800.table

下面改變一下波形的起始相位。

//point 一個週期內的點數
// 0爲最小值   maxnum爲最大值
void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
        fz = k * sin( hd * (i + 180) ) + k;			//i=0時 起點值爲0        
        tem = ( unsigned int )(fz * 1.0);						//經過係數能夠調整輸出有效值大小
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
複製代碼

生成數據以下:

30,47,67,90,117,146,177,211,246,283,321,360,400,439,478,516,553,588,622,653,682,
709,732,752,769,782,792,798,800,798,792,782,769,752,732,709,682,653,622,588,553,
516,478,439,400,360,321,283,246,211,177,146,117,90,67,47,30,17,7,1,0,1,7,17

波形爲

繼續改動一下計算公式

//point 一個週期內的點數
// 0爲最小值   maxnum爲最大值
void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
        fz = k * sin( hd * (i + 270) ) + k;			//i=0時 起點值最大       
        tem = ( unsigned int )(fz * 1.0);						//經過係數能夠調整輸出有效值大小
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
複製代碼

數據:

792,798,800,798,792,782,769,752,732,709,682,653,622,588,553,516,478,439,400,360,
321,283,246,211,177,146,117,90,67,47,30,17,7,1,0,1,7,17,30,47,67,90,117,146,177,
211,246,283,321,360,399,439,478,516,553,588,622,653,682,709,732,752,769,782,

波形:

void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
        fz = maxnum * sin( hd /2 * i );				//i=0時 sin(0)=0 起始點爲0 也就是起點值爲最小值  弧度值爲π內每一個點對應的弧度值       
        tem = ( unsigned int )(fz * 1.0);						//經過係數能夠調整輸出有效值大小
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
複製代碼

數據

0,39,78,117,156,194,232,269,306,342,377,411,444,476,507,537,565,592,618,642,665,
686,705,723,739,753,765,776,784,791,796,799,800,799,796,791,784,776,765,753,739,
723,705,686,665,642,618,592,565,537,507,476,444,411,377,342,306,269,232,194,156,
117,78,39,

波形:

繼續調整一下算法

void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
       fz = maxnum - maxnum * sin( hd /2 * i );
        tem = ( unsigned int )(fz * 1.0);						//經過係數能夠調整輸出有效值大小
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
 
複製代碼

生成數據

800,760,721,682,643,605,567,530,493,457,422,388,355,323,292,262,234,207,181,157,
134,113,94,76,60,46,34,23,15,8,3,0,0,0,3,8,15,23,34,46,60,76,94,113,134,157,181,
207,234,262,292,323,355,388,422,457,493,530,567,605,643,682,721,760

生成圖形

 

void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
    	fz = k * sin( hd * i ) + k;	
        tem = ( unsigned int )(fz * 1.0);						
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
 
複製代碼

生成數據

400,439,478,516,553,588,622,653,682,709,732,752,769,782,792,798,800,798,792,782,
769,752,732,709,682,653,622,588,553,516,478,439,400,360,321,283,246,211,177,146,
117,90,67,47,30,17,7,1,0,1,7,17,30,47,67,90,117,146,177,211,246,283,321,360,
生成波形

void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
    	fz = k * sin( hd * i + PI / 2) + k;
        tem = ( unsigned int )(fz * 1.0);						
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
複製代碼

生成數據

800,798,792,782,769,752,732,709,682,653,622,588,553,516,478,439,400,360,321,283,
246,211,177,146,117,90,67,47,30,17,7,1,0,1,7,17,30,47,67,90,117,146,177,211,246,
283,321,360,400,439,478,516,553,588,622,653,682,709,732,752,769,782,792,798

生成波形

void get_sin_tab1( unsigned int point, unsigned int maxnum )
{
    unsigned int i = 0, j = 0, k = 0;
    float hd = 0.0;        	//弧度
    float fz = 0.0;       	//峯值
    unsigned int tem = 0;
    j = point / 2;			//水平線位置 單片機沒有負電壓 水平線爲點值數量的一半
    hd = PI / j;        	// π/2 內每個點對應的弧度值
    k = maxnum / 2;      	//最大值一半
    for( i = 0; i < point; i++ )
    {
        
    	fz = k * sin( hd * i + PI / 2 + PI ) + k;
        tem = ( unsigned int )(fz * 1.0);						
        sinData[i] = tem;
        printf("%d,",tem);
    }
    	printf("\r\n");
}
複製代碼

生成數據

0,1,7,17,30,47,67,90,117,146,177,211,246,283,321,360,399,439,478,516,553,588,622
,653,682,709,732,752,769,782,792,798,800,798,792,782,769,752,732,709,682,653,622
,588,553,516,478,439,400,360,321,283,246,211,177,146,117,90,67,47,30,17,7,1

生成圖形

經過上面幾種示例能夠看出,不一樣的計算公式生成的數據也略有差別,可是本質仍是對於弧度的轉換。

相關文章
相關標籤/搜索