% gunzip -c fftw-3.3.4.tar.gz | tar -xvf -
% cd fftw-3.3.4
% ./configure
% make
% make install
$ gcc fftw.c -o fftw libfftw3.a -lm
% ./configure --prefix=/home/username
% make
% make install
% gcc fftw.c -o fftw -lm -lfftw3 -L/home/username/lib -I/home/username/include
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "fftw3.h"
#define SIZE 1000000000
main( int argc, char *argv[])
{
FILE *fp;
int i;
int N;
double *Y;
double re,im,mag,ang;
fftw_complex *in, *out;
fftw_plan p;
// データ読込み用のバッファを確保
if (!(Y=(double *)malloc(SIZE))) exit(-1);
// データ読込み(データ個数を調べる)
for (N=0; scanf("%lf",&Y[N])!=EOF && N<SIZE ;N++) ;
//N個のFFTW用の複素数配列を確保する
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
// 複素数配列に入力データをセット
for(i=0; i<N; i++) { // t=0〜1secのデータ
in[i][0]=Y[i]; // 実数部
in[i][1]=0.0; // 虚数部
}
// 1次元のフーリエ変換を実行
p = fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE);
fftw_execute(p);
// 結果を出力する
for(i=0; i<N; i++) { // f=0〜N(サンプリング周波数)のデータ
re = out[i][0]; // 複素数の実数部
im = out[i][1]; // 複素数の虚数部
mag = sqrt(re*re + im*im); // 大きさを計算
ang = atan2(im,re); // 位相角を計算
printf("%d \t %.5lf \t %.5lf \t %.5lf \t %.5lf \n", i,re,im,mag,ang);
}
// 後始末(使用した配列等を廃棄)
fftw_destroy_plan(p);
fftw_free(in);
fftw_free(out);
free(Y);
}
% gunzip -c gnuplot-4.6.5.tar.gz | tar -xvf -
% cd gnuplot-4.6.5
% ./configure --prefix=/home/username
% make
% make install
set terminal png
set nokey
set xlabel "f[kHz]"
set ylabel "|F(w)|"
set logscale x
set logscale y
plot [:][:] 'fftw.dat' using ($1/1000):4 with impulses linewidth 1
$ gnuplot gp_out > hoge.png
#!/home/username/bin/gnuplot
set terminal png
set nokey
set xlabel "f[kHz]"
set ylabel "|F(w)|"
set logscale x
set logscale y
plot [:][:] '< cat -' using ($1/1000):4 with impulses linewidth 1
$ singen 1000 | fftw | gp_fft > hoge.png
#!/home/username/bin/gnuplot
set terminal png
set nokey
set xlabel "f[kHz]"
set ylabel "|F(w)|"
set logscale y
plot [:][:] '< cat -' using ($1/1000):4 with impulses linewidth 1
#!/home/username/bin/gnuplot
set terminal png
set nokey
set xlabel "f[kHz]"
set ylabel "|F(w)|"
plot [:][:] '< cat -' using ($1/1000):4 with impulses linewidth 1
#!/home/username/bin/gnuplot
set terminal png
set nokey
set xlabel "f[kHz]"
set ylabel "Angle"
set logscale x
plot [:][:] '< cat -' using ($1/1000):5 with line linewidth 2
#!/home/username/bin/gnuplot
set terminal png
set nokey
plot [:][:] '< cat -' with line linewidth 5
#!/home/username/bin/gnuplot
set terminal png
set nokey
set style line 1 pointtype 3
plot [:][:] '< cat -' linestyle 1
#!/home/username/bin/gnuplot
set terminal png
set nokey
plot [:][:] '< cat -' with impulses linewidth 5
#include <stdio.h>
#include <math.h>
double last;
double unwrap(double in)
{
double div;
while (in-last < -2*M_PI)
in += 2*M_PI;
while (in-last > 2*M_PI)
in -= 2*M_PI;
div = in - last;
if (div < -M_PI)
in += 2*M_PI;
else if (div > M_PI)
in -= 2*M_PI;
last = in;
return in;
}
int main(int argc, char *argv[])
{
int i;
double re,im,mag,ang;
while (scanf("%d %lf %lf %lf %lf",&i,&re,&im,&mag,&ang) != EOF) {
printf("%d \t %.5lf \t %.5lf \t %.5lf \t %.5lf\n",
i,re,im,mag,unwrap(ang));
}
return 0;
}
% white0 1000 | lpf 0.5 | fftw | fftw_unwrap | gp_ang > lpf_ang.png
#include<stdio.h>
#include<math.h>
#define SAMPLE 1000000
#define MAX 32767
#define POINT 1000000
main( int argc, char *argv[] )
{
double d,f;
double out;
int t;
f = atoi(argv[1]);
d = 2*M_PI*f/SAMPLE;
for (t=0; t<POINT ;t++) {
out = sin(t*d);
printf("%d\n",(int)(out*MAX/2));
}
}
% singen 1000 | head -5000 | gp_wav > singen.png
% singen 1000 | fftw | gp_fft > singen_fft.png
% singen 1000 | head -999999 | fftw | gp_fft > singenx_fft.png
f = S/N * d
d = N/S * f
N = d/f * S
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//1秒分のデータを生成
#define FREQ (1000) //生成周波数
#define SAMPLE (1000000) //サンプリング周波数
#define N (1000*4) //サンプル数
double sin_table[N/4+1];
void make_table(void)
{
int x;
for (x=0; x<=N/4 ;x++)
sin_table[x] = sin(2*M_PI/N*x);
}
double sin_x(int x)
{
int xx;
xx = x%(N/4);
if (x < N/4*1) return sin_table[xx];
else if (x < N/4*2) return sin_table[N/4-xx];
else if (x < N/4*3) return -sin_table[xx];
else return -sin_table[N/4-xx];
}
main( int argc, char *argv[] )
{
int i, d, t=0;
double f, s;
f = FREQ;
s = SAMPLE;
make_table();
if (argc>1)
f = atof(argv[1]);
if (argc>2)
s = atof(argv[2]);
d = f*N/s;
for (i=0; i<s ;i++) {
printf("%f\n",sin_x(t));
t = (t+d)%N;
}
}
% dds 1000 | head -5000 | gp_wav > dds.png
% dds 1000 | fftw | gp_fft > dds_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//1秒分のデータを生成
#define FREQ (1000) //生成周波数
#define SAMPLE (1000000) //サンプリング周波数
#define N (1000*4) //サンプル数
int square_x(int x)
{
int xx;
if (x < N/4*1) return 1;
else if (x < N/4*2) return 1;
else if (x < N/4*3) return -1;
else return -1;
}
main( int argc, char *argv[] )
{
int i, d, t=0;
double f, s;
f = FREQ;
s = SAMPLE;
if (argc>1)
f = atof(argv[1]);
if (argc>2)
s = atof(argv[2]);
d = f*N/s;
for (i=0; i<s ;i++) {
printf("%d\n",square_x(t));
t = (t+d)%N;
}
}
% square 1000 | head -5000 | gp_wav > square.png
% square 1000 | fftw | gp_fft > square_fft.png
% squarex 1000 | head -5000 | gp_wav > squarex.png
% squarex 1000 | fftw | gp_fft > squarex_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//1秒分のデータを生成
#define FREQ (1000) //生成周波数
#define SAMPLE (1000000) //サンプリング周波数
#define N (1000*4) //サンプル数
double tri_table[N/4+1];
void make_table(void)
{
int x;
for (x=0; x<=N/4 ;x++)
tri_table[x] = 4.0/N*x;
}
double tri_x(int x)
{
int xx;
xx = x%(N/4);
if (x < N/4*1) return tri_table[xx];
else if (x < N/4*2) return tri_table[N/4-xx];
else if (x < N/4*3) return -tri_table[xx];
else return -tri_table[N/4-xx];
}
main( int argc, char *argv[] )
{
int i, d, t=0;
double f, s;
f = FREQ;
s = SAMPLE;
make_table();
if (argc>1)
f = atof(argv[1]);
if (argc>2)
s = atof(argv[2]);
d = f*N/s;
for (i=0; i<s ;i++) {
printf("%f\n",tri_x(t));
t = (t+d)%N;
}
}
% triangle 1000 | head -5000 | gp_wav > triangle.png
% triangle 1000 | fftw | gp_fft > triangle_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//1秒分のデータを生成
#define FREQ (1000) //生成周波数
#define SAMPLE (1000000) //サンプリング周波数
#define N (1000*4) //サンプル数
double saw_table[N/2+1];
void make_table(void)
{
int x;
for (x=0; x<=N/2 ;x++)
saw_table[x] = 2.0/N*x;
}
double saw_x(int x)
{
int xx;
xx = x%(N/2);
if (x < N/2*1) return saw_table[xx];
else return -saw_table[N/2-xx];
}
main( int argc, char *argv[] )
{
int i, d, t=0;
double f, s;
f = FREQ;
s = SAMPLE;
make_table();
if (argc>1)
f = atof(argv[1]);
if (argc>2)
s = atof(argv[2]);
d = f*N/s;
for (i=0; i<s ;i++) {
printf("%f\n",saw_x(t));
t = (t+d)%N;
}
}
% saw 1000 | head -5000 | gp_wav > saw.png
% saw 1000 | fftw | gp_fft > saw_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//1秒分のデータを生成
#define SAMPLE (1000000) //サンプリング周波数
double Uniform( void ){
return ((double)rand()+1.0)/((double)RAND_MAX+2.0);
}
main( int argc, char *argv[] )
{
int i;
srand(777);
for (i=0; i<SAMPLE ;i++) {
printf("%f\n",Uniform());
}
}
% rand | head -100 | gp_wav > rand.png
% rand | fftw | gp_fft > rand_fft.png
#include<stdio.h>
#include<math.h>
//1秒分のデータを生成
#define SAMPLE (1000000) //サンプリング周波数
int stage = 1023;
int pn16(data,tap)
int data,tap;
{
int mask,line,i;
data = data & stage;
mask = data & tap;
line = 0;
for (i=0; i<24 ;i++) {
line += mask & 0x01;
mask = mask>>1;
}
data = data<<1;
if (line & 0x01) {
data = data|1;
}
return(data&stage);
}
main( int argc, char *argv[] )
{
int pn=0x3ff; // 初期値(全ビット1)
int i;
for (i=0; i<SAMPLE ;i++) {
pn = pn16(pn,0x204);
printf("%d\n",pn&0x3ff); // 乱数出力
}
}
% pngen | head -100 | gp_wav > pngen.png
→意外と変な波形だ。
% pngen | fftw | gp_fft > pngen_fft.png
#include<stdio.h>
#include<math.h>
main( int argc, char *argv[] )
{
unsigned int in;
while (scanf("%d", &in) != EOF) {
printf("%d\n",in&0x01);
}
}
% pngen | bit0 | head -100 | gp_wav > pngen0.png
% pngen | bit0 | fftw | gp_fft > pngen0_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main( int argc, char *argv[] )
{
FILE *fp;
double in1,in2;
fp = fopen(argv[1],"r");
if (fp) {
while((fscanf(stdin,"%lf",&in1)!=EOF)&&(fscanf(fp,"%lf",&in2)!=EOF)) {
printf("%f\n",in1+in2);
}
}
else {
in2 = atof(argv[1]);
while((fscanf(stdin,"%lf",&in1)!=EOF)) {
printf("%f\n",in1+in2);
}
}
}
% dds 1000 > dds1000.dat
% dds 2000 | add dds1000.dat | head -5000 | gp_wav > add_1000_2000.png
% dds 2000 | add dds1000.dat | fftw | gp_fft > add_1000_2000_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main( int argc, char *argv[] )
{
FILE *fp;
double in1,in2;
fp = fopen(argv[1],"r");
if (fp) {
while((fscanf(stdin,"%lf",&in1)!=EOF)&&(fscanf(fp,"%lf",&in2)!=EOF)) {
printf("%f\n",in1*in2);
}
}
else {
in2 = atof(argv[1]);
while((fscanf(stdin,"%lf",&in1)!=EOF)) {
printf("%f\n",in1*in2);
}
}
}
% dds 1000 > dds1000.dat
% dds 2000 | mul dds1000.dat | head -5000 | gp_wav > mul_1000_2000.png
% dds 2000 | mul dds1000.dat | fftw | gp_fft > mul_1000_2000_fft.png
% dds 1250 | mul dds1000.dat | head -5000 | gp_wav > mul_1000_1250.png
% dds 1250 | mul dds1000.dat | fftw | gp_fft > mul_1000_1250_fft.png
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main( int argc, char *argv[] )
{
FILE *fp;
double in1,in2;
fp = fopen(argv[1],"r");
if (fp) {
while((fscanf(stdin,"%lf",&in1)!=EOF)&&(fscanf(fp,"%lf",&in2)!=EOF)) {
printf("%d\n",in1>in2);
}
}
else {
in2 = atof(argv[1]);
while((fscanf(stdin,"%lf",&in1)!=EOF)) {
printf("%d\n",in1>in2);
}
}
}
% triangle 1000 | head -5000 | comp 0.5 | gp_wav > comp05.png
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<math.h>
main( int argc, char *argv[] )
{
FILE *fp;
double in, min=DBL_MAX, max=-DBL_MAX;
while((fscanf(stdin,"%lf",&in)!=EOF)) {
if (min>in) min = in;
if (max<in) max = in;
}
printf("%f, %f\n",min,max);
}
% triangle 1000 | minmax
-0.997000, 0.997000
# gcc -m32 txt2wav.c -o txt2wav -lm
% dds 1000 50000 | add 1 | mul 128 | int > dds1000x.dat
% txt2wav -50000 dds1000x.dat
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main( int argc, char *argv[] )
{
double in;
while((fscanf(stdin,"%lf",&in)!=EOF)) {
printf("%d\t%d\n",(int)floor(in),(int)floor(in));
}
}
% dds 1000 44100 | mul 32767 | stereo > dds1000s.dat
% txt2wav -44100 dds1000s.dat
% dumpwave dds1000x.wav > dds1000x.txt
% gunzip -c lame-3.99.5.tar.gz | tar -xvf -
% cd lame-3.99.5
% ./configure --prefix=/home/username
% make
% make install
% lame dds1000x.wav dds1000x.mp3
% gunzip -c sox-14.4.1.tar.gz | tar -xvf -
% cd sox-14.4.1
% ./configure --prefix=/home/username
% make
% make install
option | description |
---|---|
-s | signed |
-u | unsigned |
-b | 8bit |
-w | 16bit |
-l | 32bit |
-d | 64bit |
-r rate | Sampling Rate |
-c ch | Channel(ch:1,2) |
-x | exchange endian |
-t type | format type |
# sox dds1000x.wav dds1000x.mp3
% dds 1000 50000 | add 1 | mul 128 | int > dds1000x.dat
% txt2wav -50000 dds1000x.dat
% lame dds1000x.wav dds1000x.mp3
% dds 1000 50000 | add 1 | mul 128 | int | head -250 | gp_wav > dds1000x.png
% dds 1000 50000 | add 1 | mul 128 | int | fftw | gp_fft > dds1000x_fft.png
% triangle 1000 50000 | add 1 | mul 128 | int > tri1000x.dat
% txt2wav -50000 tri1000x.dat
% lame tri1000x.wav tri1000x.mp3
% triangle 1000 50000 | add 1 | mul 128 | int | head -250 | gp_wav > tri1000x.png
% triangle 1000 50000 | add 1 | mul 128 | int | fftw | gp_fft > tri1000x_fft.png
% triangle 1000 50000 | add 1 | mul 128 | fftw | gp_fft > tri1000xx_fft.png
triangle 1000 50000 | mul 128 | int | add 128 | fftw | gp_fft > tri1000xxx_fft.png
% triangle 1000 50000 | mul 128 | int | add 128> tri1000xxx.dat
% txt2wav -50000 tri1000xxx.dat
% lame tri1000xxx.wav tri1000xxx.mp3
% triangle 1000 50000 | mul 128 | int | add 128 | head -250 | gp_wav > tri1000xxx.png
% triangle 1000 50000 | mul 128 | int | add 128 | fftw | gp_fft > tri1000xxx_fft.png
% square 1000 50000 | add 1 | mul 127 | int > squ1000x.dat
% txt2wav -50000 squ1000x.dat
% lame squ1000x.wav squ1000x.mp3
% square 1000 50000 | add 1 | mul 127 | int | head -250 | gp_wav > squ1000x.png
% square 1000 50000 | add 1 | mul 127 | int | fftw | gp_fft > squ1000x_fft.png
% saw 1000 50000 | add 1 | mul 128 | int > saw1000x.dat
% txt2wav -50000 saw1000x.dat
% lame saw1000x.wav saw1000x.mp3
% saw 1000 50000 | add 1 | mul 128 | int | head -250 | gp_wav > saw1000x.png
% saw 1000 50000 | add 1 | mul 128 | int | fftw | gp_fft > saw1000x_fft.png
% rand | head -50000 | add 1 | mul 128 | int > randx.dat
% txt2wav -50000 randx.dat
% lame randx.wav randx.mp3
% rand | head -50000 | add 1 | mul 128 | int | head -100 | gp_wav > randx.png
% rand | head -50000 | add 1 | mul 128 | int | fftw | gp_fft > randx_fft.png
% pngen | bit0 | head -50000 | mul 255 > pngen0x.dat
% txt2wav -50000 pngen0x.dat
% lame pngen0x.wav pngen0x.mp3
% pngen | bit0 | head -50000 | mul 255 | head -100 | gp_wav > pngen0x.png
% pngen | bit0 | head -50000 | mul 255 | fftw | gp_fft > pngen0x_fft.png
% dds 1000 | add 1 | mul 127 | int > dds1000xx.dat
% txt2wav -1000000 dds1000xx.dat
% lame dds1000xx.wav dds1000xx.mp3
% dds 1000 | add 1 | mul 127 | int | head -5000 | gp_wav > dds1000xx.png
% dds 1000 | add 1 | mul 127 | int | fftw | gp_fft > dds1000xx_fft.png
% dds 1000 | mul 127 | int | add 128 > dds1000xxx.dat
% txt2wav -1000000 dds1000xxx.dat
% lame dds1000xxx.wav dds1000xxx.mp3
% dds 1000 | mul 127 | int | add 128 | head -5000 | gp_wav > dds1000xxx.png
% dds 1000 | mul 127 | int | add 128 | fftw | gp_fft > dds1000xxx_fft.png
% dds 1000 | comp 0 | head -2000 | gp_wav > comp1000.png
% dds 1000 | comp 0 | fftw | gp_fft > comp1000_fft.png
% dds 1000 | comp 0 | mul 255 > comp1000.dat
% txt2wav -1000000 comp1000.dat
% lame comp1000.wav comp1000.mp3
% triangle 10000 > tri10000.dat
% dds 1000 | comp tri10000.dat | head -2000 | gp_wav > pwm1000.png
% dds 1000 | comp tri10000.dat | fftw | gp_fft > pwm1000_fft.png
% dds 1000 | comp tri10000.dat | mul 255 > pwm1000.dat
% txt2wav -1000000 pwm1000.dat
% lame pwm1000.wav pwm1000.mp3
% triangle 40000 > tri40000.dat
% dds 1000 | comp tri40000.dat | fftw | gp_fft > pwm1000x_fft.png
% dds 1000 | comp tri40000.dat | mul 255 > pwm1000x.dat
% txt2wav -1000000 pwm1000x.dat
% lame pwm1000x.wav pwm1000x.mp3
% triangle 300000 > tri300000.dat
% dds 1000 | comp tri300000.dat | fftw | gp_fft > pwm1000xx_fft.png
% dds 1000 | comp tri300000.dat | mul 255 > pwm1000xx.dat
% txt2wav -1000000 pwm1000xx.dat
% lame pwm1000xx.wav pwm1000xx.mp3
% rand | mul 2 | add -1 > rand.dat
% dds 1000 | comp rand.dat | head -2000 | gp_wav > rpwm1000.png
% dds 1000 | comp rand.dat | fftw | gp_fft > rpwm1000_fft.png
% dds 1000 | comp rand.dat | mul 255 > rpwm1000.dat
% txt2wav -1000000 rpwm1000.dat
% lame rpwm1000.wav rpwm1000.mp3
#include <stdio.h>
int main(int argc, char *argv[])
{
int out;
double in, delta, sigma=0;
while (scanf("%lf", &in) != EOF) {
delta = in - out;
sigma += delta;
out = (sigma > 0) ? 1 : -1;
printf("%d\n",out);
}
return 0;
}
% dds 1000 | ds1 | head -2000 | gp_wav > ds1_1000.png
% dds 1000 | ds1 | fftw | gp_fft > ds1_1000_fft.png
% dds 1000 | ds1 | add 1 | mul 127 | int > ds1_1000.dat
% txt2wav -1000000 ds1_1000.dat
% lame ds1_1000.wav ds1_1000.mp3
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX_SAMPLE (1000000)
#define SAMPLE (1000)
double sin_table[MAX_SAMPLE/4+1];
int t[MAX_SAMPLE];
int N, f1, f2;
void make_table(void)
{
int x;
for (x=0; x<=N/4 ;x++)
sin_table[x] = sin(2*M_PI/N*x);
}
double sin_x(int x)
{
int xx;
xx = x%(N/4);
if (x < N/4*1) return sin_table[xx];
else if (x < N/4*2) return sin_table[N/4-xx];
else if (x < N/4*3) return -sin_table[xx];
else return -sin_table[N/4-xx];
}
main( int argc, char *argv[] )
{
int i, j, d;
double out;
N = SAMPLE;
if (argc>1)
N = atoi(argv[1]);
f1 = 0;
f2 = N/2-1;
if (argc>2)
f2 = atoi(argv[2]);
if (argc>3)
f1 = atoi(argv[3]);
make_table();
srand(777);
for (i=0; i<N ;i++) t[i] = rand()%N; // 位相の初期値を0ではなく、バラつかせる
for (i=0; i<N ;i++) {
out = 0;
for (j=f1; j<=f2 ;j++) {
out += sin_x(t[j]);
d = j;
t[j] = (t[j]+d)%N;
}
printf("%f\n",out);
}
}
% white0 1000 | fftw | gp_fft > white01000_fft.png
% white0 1000 | head -100 | gp_wav > white01000_100.png
% white0 1000 | gp_wav > white01000.png
% white0 1000 | fftw | fftw_unwrap | gp_ang > white01000_ang.png
% white0 1000 | rcf 0.5 | fftw |fftw_unwrap | gp_ang > rcf_ang.png
for (i=0; i<N ;i++) t[i] = 0;
srand(777);
for (i=0; i<N ;i++) t[i] = rand()%N;
% white 1000 | head -100 | gp_wav > white1000_100.png
% white 1000 | fftw | gp_fft > white1000_fft.png
% white0 1000 | minmax
-318.308839, 318.308839
% white 1000 | minmax
-51.121728, 45.900439
% white 48000 | head -100 | gp_wav > white48000_100.png
% white 48000 | fftw | gp_fft > white48000_fft.png
% white 48000 | minmax
-451.917214, 435.368105
% white 48000 | mul 70 > white48000.dat
% txt2wav -48000 white48000.dat
% lame white48000.wav white48000.mp3
% white 1000 100 10 | fftw | gp_fft > white1000_100_10_fft.png
% white 1000 100 10 | mul 5 | add 128 > white1000_100_10.dat
% txt2wav -1000 white1000_100_10.dat
% lame white1000_100_10.wav white1000_100_10.mp3