欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sin_tone

程序员文章站 2024-03-03 19:17:04
...

matlab输出sin_table表

clear all;close all;clc
f = 10;   % 频率, 10Hz
T = 1/f;
dt=T/1600;  
N=1600; 
x = 0:dt:(N-1)*dt;
w = f*2*pi; % 频率,rad/s
A = 0.25;      % 振幅
sin_wave =
A*sin(w*x);
%audiowrite('D:\\sinwave.wav',sin_wave,16000);
%dlmwrite('D://3.txt',sin_wave);
figure;
plot(x,sin_wave);
grid on
xlabel('x/s');
ylabel('y/V');
% 
% % 量化位宽
width = 16;%数据宽度16位
% 量化滤波器系数
sin_data  = round(sin_wave .* (2^(width-1) - 1));%量化正弦波形数据并取整
data_com_sin =
zeros(1,length(sin_data));
for i =
1:length(sin_data)
    if sin_data(i) >= 0
        data_com_sin(i) = sin_data(i);
    else
       data_com_sin(i) =- sin_data(i-length(sin_data)/2);
    end
end
dlmwrite('D://sin_table.txt',data_com_sin);
% data_bin =
dec2bin(data_com_sin);%十进制转二进制

图像:sin_tone

c语言直接生成

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
void usage(){
fprintf(stderr,"tone <frequency_Hz>,[<amplitude>]
[<frequency_Hz>,[<amplitude>]...]\n");
exit(1);
}
int main (int argc,char *argv[]){
 int i,j;
double *f;
double *amp;
   if(argc<2)usage();
f=alloca(sizeof(*f)*(argc-1));
amp=alloca(sizeof(*amp)*(argc-1));
i=0;
while(argv[i+1]){
char *pos=strchr(argv[i+1],',');
i]=atof(argv[i+1]);
if(pos)  
amp[i]=atof(pos+1)*32767.f;
else
amp[i]=32767.f;
tf(stderr,"%g Hz, %g amp\n",f[i],amp[i]);
i++;
  }
for(i=0;i<44100*10;i++){
float val=0;
int ival;
for(j=0;j<argc-1;j++)
val+=amp[j]*sin(i/44100.f*f[j]*2*M_PI);
ival=rint(val);
if(ival>32767.f)ival=32767.f;
if(ival<-32768.f)ival=-32768.f;
fprintf(stdout,"%c%c%c%c",
            (char)(ival&0xff),
        
(char)((ival>>8)&0xff),
         
(char)(ival&0xff),
      
(char)((ival>>8)&0xff));
  } 
return(0);
}

推荐阅读