Linux下安装gnuplot
sudo apt-get install gnuplot
安装完成后终端打印gnuplot 有下面打印即可
生成数据的代码
#include <stdio.h>
#include <math.h>
#define ANGLE_RESOLUTION 500 // Number of angle points to calculate
int main(void)
{
int numElements = 4; // Number of array elements
double spacing = 0.2; // Element separation in metres
double freq = 1000.0; // Signal frequency in Hz
double speedSound = 343.0; // m/s
int a;
int i;
// Iterate through arrival angle points
for (a=0 ; a<ANGLE_RESOLUTION ; a++)
{
// Calculate the planewave arrival angle
double angle = -90 + 180.0 * a / (ANGLE_RESOLUTION-1);
double angleRad = M_PI * (double) angle / 180;
double realSum = 0;
double imagSum = 0;
// Iterate through array elements
for (i=0 ; i<numElements ; i++)
{
// Calculate element position and wavefront delay
double position = i * spacing;
double delay = position * sin(angleRad) / speedSound;
// Add Wave
realSum += cos(2.0 * M_PI * freq * delay);
imagSum += sin(2.0 * M_PI * freq * delay);
}
double output = sqrt(realSum * realSum + imagSum * imagSum) / numElements;
double logOutput = 20 * log10(output);
if (logOutput < -50) logOutput = -50;
printf("%d %f %f %f %f\n", a, angle, angleRad, output, logOutput);
}
return 0;
}
编译代码
gcc -o beamPattern beamPattern.c -lm
生成数据到文件
./beamPattern > beamPattern.dat
绘图代码 beamPattern.gnuplot
reset
unset key
set xlabel "Arrival Angle (degrees)" font "arial,12"
set ylabel "Gain (dB)" font "arial,12"
set grid lc rgbcolor "#BBBBBB"
plot 'beamPattern.dat' u 2:5 w l
绘图完成
注意:使用windows下的数据源去linux下执行也会报这个错误
"beamPattern.gnuplot", line 6: x range is invalid
使用linux下编译生成的数据源就没这个问题 在linux和window下都能生成图像