libdspl-2.0
Digital Signal Processing Algorithm Library
Plotting data by Gnuplot interface.

Functions

int DSPL_API gnuplot_create (int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
 Create GNUPLOT chart. More...
 
void DSPL_API gnuplot_close (void *h)
 Close GNUPLOT handle. More...
 
void DSPL_API gnuplot_cmd (void *h, char *cmd)
 Function sends cmd command to GNUPLOT corresponds to h handle. More...
 
int DSPL_API gnuplot_open (void **hplot)
 Open GNUPLOT program. More...
 

Detailed Description

Function Documentation

◆ gnuplot_close()

void gnuplot_close ( void *  h)

Close GNUPLOT handle.


This function closes the previously opened GNUPLOT handle after transferring all commands.

Note
From a system point of view, h is a pointer to an open file in which You can write commands for execution by the GNUPLOT package. This function closes this file.
It is important to note that closing the handle does not mean closing the plot window. After closing the package handle, the user cannot send build commands, but the plot window remains open, as it is processed by the package GNUPLOT regardless.
Parameters
[in]hGNUPLOT handle which will be closed.

Author
Sergey Bakhurin www.dsplib.org

Definition at line 325 of file gnuplot.c.

◆ gnuplot_cmd()

void gnuplot_cmd ( void *  h,
char *  cmd 
)

Function sends cmd command to GNUPLOT corresponds to h handle.


Note
From a system point of view, h is a pointer to an open file, into which you can write commands for execution by the GNUPLOT package. This function writes the string cmd to the given file.

From the user's point of view, calling the gnuplot_cmd function is GNUPLOT script one line executing.

A comprehensive description of the graphing capabilities of the GNUPLOT package, with examples of commands can be found on GNUPLOT project .
Parameters
[in]hGNUPLOT handle.

[in]cmdGNUPLOT command string.

Author
Sergey Bakhurin www.dsplib.org

Definition at line 390 of file gnuplot.c.

Referenced by gnuplot_create().

◆ gnuplot_create()

int gnuplot_create ( int  argc,
char *  argv[],
int  w,
int  h,
char *  fn_png,
void **  hplot 
)

Create GNUPLOT chart.


This function opens the GNUPLOT package depending on the argv parameters passed to the program. After calling this function, the handle of the graph will be written to the address hplot and it becomes possible to send GNUPLOT commands to display the graphs.

Note
From a system point of view, hplot is a pointer to an open file in which you can write commands for execution by the GNUPLOT package.
Parameters
[in]argcThe number of arguments to invoke the program.
(number of argv variables).
This value cannot be less than one, because argv [0] keeps the program executable file name.

[in]argvPointer to a strings array of the executable file parameters .
The size of the array is argc.
argv [0] keeps the executable file name.
argv [1] sets the graph display format:
--plotwin   display the graph in a individual window
            (this parameter is used by default);

--plotpng   save the plot to a `fn_png` png file;

--noplot    plot not to create, ignore all GNUPLOT commands.

[in]wThe width of the graph window or png file in pixels.

[in]hThe height of the graph window or png file in pixels.

[in]fn_pngPlot png-file name.
[in,out]hplotPointer to the handle address of the GNUPLOT package.
A pointer to the current graph will be recorded at this address. This pointer is required to send GNUPLOT commands.

Returns
RES_OK if function is calculated successfully.
Else code error. The hplot pointer sets in NULL if function returns error.
Note
If argv[1] parameter equals --noplot, then function returns RES_OK, but hplot will be set as NULL.
GNUPLOT handle must be closed by gnuplot_close after plotting.
An example of plotting sine and cosine is given in the following listing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 100
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot[3]; /* GNUPLOT handles */
hdspl = dspl_load(); /* Load DSPL function */
double x[N];
double s[N]; /* s(x) = sin(x) */
double c[N]; /* c(x) = cos(x) */
int n;
int err;
/* x vector from -4*pi to 4*pi */
linspace(-4.0 * M_PI, 4 * M_PI, N , DSPL_SYMMETRIC, x);
for(n = 0; n < N; n++)
{
s[n] = sin(x[n]); /* s(x) = sin(x) */
c[n] = cos(x[n]); /* c(x) = cos(x) */
}
/* Save to files "dat/sine.txt" and "dat/cosine.txt" */
writetxt(x, s, N, "dat/sine.txt");
writetxt(x, c, N, "dat/cosine.txt");
/* plotting by GNUPLOT */
/* Create window 0 */
err = gnuplot_create(argc, argv, 560, 280, "img/gnuplot_script_sin.png",
hplot);
printf("GNUPLOT err = %d\n", err);
gnuplot_cmd(hplot[0], "set grid");
gnuplot_cmd(hplot[0], "set xlabel 'x'");
gnuplot_cmd(hplot[0], "set ylabel 'sin(x)'");
gnuplot_cmd(hplot[0], "plot 'dat/sine.txt' with lines title 'sin(x)'");
gnuplot_close(hplot[0]);
/* Create window 1 */
err = gnuplot_create(argc, argv, 560, 280, "img/gnuplot_script_cos.png",
hplot+1);
printf("GNUPLOT err = %d\n", err);
gnuplot_cmd(hplot[1], "set grid");
gnuplot_cmd(hplot[1], "set xlabel 'x'");
gnuplot_cmd(hplot[1], "set ylabel 'cos(x)'");
gnuplot_cmd(hplot[1], "plot 'dat/cosine.txt' with lines title 'cos(x)'");
gnuplot_close(hplot[1]);
/* Create window 2 */
err = gnuplot_create(argc, argv, 560, 280, "img/gnuplot_script_sincos.png",
hplot+2);
printf("GNUPLOT err = %d\n", err);
gnuplot_cmd(hplot[2], "set grid");
gnuplot_cmd(hplot[2], "set xlabel 'x'");
gnuplot_cmd(hplot[2], "set ylabel 'sin(x), cos(x)'");
gnuplot_cmd(hplot[2], "plot 'dat/sine.txt' with lines title 'sin(x)', \\");
gnuplot_cmd(hplot[2], " 'dat/cosine.txt' with lines title 'cos(x)");
gnuplot_close(hplot[2]);
dspl_free(hdspl); /* free dspl handle */
return 0;
}

This program calculates the values of the sine and cosine functions, and also saves the text files dat / cosine.txt anddat / sine.txt.
The saved data is displayed on three graphs (with sine, cosine data by both data):

Author
Sergey Bakhurin www.dsplib.org

Definition at line 200 of file gnuplot.c.

◆ gnuplot_open()

int gnuplot_open ( void **  hplot)

Open GNUPLOT program.


This function opens the GNUPLOT package. After calling this function, the handle of the GNUPLOT will be written to the address hplot and it becomes possible to send GNUPLOT commands.

Note
From a system point of view, hplot is a pointer to an open file in which you can write commands for execution by the GNUPLOT package.
Parameters
[in,out]hplotPointer to the handle address of the GNUPLOT package.
This pointer is required to send GNUPLOT commands.

Returns
RES_OK if function is calculated successfully.
Else code error. The hplot pointer sets in NULL if function returns error.
GNUPLOT handle must be closed by gnuplot_close after plotting.
An example of plotting sine and cosine is given in the following listing:
Note
The difference between gnuplot_open andgnuplot_create is that gnuplot_create processes the program execution parameters and creates a GNUPLOT terminal.
The gnuplot_open function opens the GNUPLOT handle to be able to send commands, regardless of program execution parameters, but does not create terminals.
Author
Sergey Bakhurin www.dsplib.org

Definition at line 482 of file gnuplot.c.

void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot.c:325
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot.c:390
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: inout.c:491
int DSPL_API linspace(double x0, double x1, int n, int type, double *x)
Function fills a vector with n linearly spaced elements between x0 and x1.
Definition: array.c:1009
int DSPL_API gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
Definition: gnuplot.c:200