c语言写采样函数 采样函数定义( 二 )


#includestdlib.h
main()
{
int i,j;
srand((int)time(0));
for(i=0;i10;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
printf(" %d ",j);
}
}
执行:与rand范例比较
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
又或:
用"int x = rand() % 100;"来生成 0 到 100 之间的随机数这种方法是不或取的 , 比较好的做法是: j=(int)(n*rand()/(RAND_MAX+1.0))产生一个0到n之间的随机数
int main(void)
{
int i;
time_t t;
srand((unsigned) time(t));
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i10; i++)
printf("%d\n", rand() % 100);
return 0;
}
除以上所说的之外,补充一点就是srand这个函数一定要放在循环外面或者是循环调用的外面,否则的话得到的是相同的数字 。
MSDN中的例子 。
// crt_rand.c
// This program seeds the random-number generator
// with the time, then displays 10 random integers.
//
#include stdlib.h
#include stdio.h
#include time.h
int main( void )
{
int i;
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
//
srand( (unsigned)time( NULL ) );
// Display 10 numbers.
for( i = 0;i10;i++ )
printf( "%6d\n", rand() );
printf("\n");
// Usually, you will want to generate a number in a specific range,
// such as 0 to 100, like this:
{
int RANGE_MIN = 0;
int RANGE_MAX = 100;
for (i = 0;i10; i++ )
{
int rand100 = (((double) rand() /
(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
printf( "%6d\n", rand100);
}
}
总结:
c语言写采样函数我们知道rand()函数可以用来产生随机数,但是这不是真真意义上的随机数 , 是一个伪随机数,是根据一个数,我们可以称它为种了 , 为基准以某个递推公式推算出来的一系数,当这系列数很大的时候 , 就符合正态公布 , 从而相当于产生了随机数,但这不是真正的随机数 , 当计算机正常开机后,这个种子的值是定了的 , 除非你破坏了系统,为了改变这个种子的值,C提供了 srand()函数,它的原形是void srand( int a) 功能是
初始化随机产生器既rand()函数的初始值,即使把种子的值改成a; 从这你可以看到通过sand()函数 , 我们是可以产生可以预见的随机序列 , 
那我们如何才能产生不可预见的随机序列呢c语言写采样函数?我们可能常常需要这样的随机序列,是吧 。利用srand((unsign)(time(NULL))是一种方法 , 因为每一次运行程序的时间是不同的,对了,你知道time() 函数的功能是返回从1970/01/01到现在的秒数的吧,可能这个起始时间不正确,你查一下对不对吧,C还提供了另一个更方便的函数,randomize()
原形是void randomize(),功能是用来始初rand() 的种子的初始值,而且该值是不确定的 , 它相当于srand((unsign)(time(NULL)) 不过应注意的是randomize()的功能要通过time来实现所以在调用它时头文件要包含time.h罢了
用c语言编程采样载波 载波为正弦波,采样率为20MHz,模拟信号频率为1.25MHz?下面是一个简单c语言写采样函数的用 C 语言实现采样正弦波的示例代码:
cCopy code#include stdio.h#include math.h#define PI 3.14159265358979323846int main(){int sample_rate = 20000000;// 采样率
int signal_freq = 1250000;// 模拟信号频率
int num_samples = 100;// 采样点数量
double t, signal, carrier, modulated;int i, j;// 生成载波
double carrier_freq = signal_freq * 16; // 载波频率为信号频率的16倍

推荐阅读