Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active August 13, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/6e30c735019e4e3c5e05e17bc2f5458c to your computer and use it in GitHub Desktop.
Save harieamjari/6e30c735019e4e3c5e05e17bc2f5458c to your computer and use it in GitHub Desktop.
To model a vibrating string using gnuplot and then ffmpeg.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#define TIME_OF_VIDEO (uint32_t) 10
#define LENGTH_OF_STRING (uint32_t) 3
#define SAMPLE_PER_LENGTH (uint32_t) 50
#define FPS (uint32_t) 30
#define FREQUENCY (uint32_t) 1
#define MODE (uint32_t) 3
double my_func(double t, double x){
return (double) (3*sin(2*M_PI*FREQUENCY*t)*sin(x*MODE*M_PI/LENGTH_OF_STRING));
}
int main(){
FILE *fp = NULL;
char command[300];
double length_period = (double) 1/SAMPLE_PER_LENGTH;
double fps_period = (double) 1/FPS;
for (uint32_t frame = 0; frame < TIME_OF_VIDEO*FPS; frame++){
fp = fopen("pl.dat", "w");
if (!fp) return -1; // check with `echo $?`
for (uint32_t x = 0; x < LENGTH_OF_STRING*SAMPLE_PER_LENGTH; x++){
fprintf(fp, "%.3f %.3f\n", (double) x*length_period, my_func((double)frame*fps_period, (double) x));
}
fclose(fp);
sprintf(command, "gnuplot -e \"set term png; set xlabel \'plot-%d.png\'; plot [0:%d] [-3:3] \'pl.dat\' with lines\" > plot-%03d", frame, LENGTH_OF_STRING. frame); >
printf("%s\n", command);
system(command);
}
return 0; // check with `echo $?`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment