// PROGRAM analyze // フーリエー係数 a_k と b_k を求める #include "TrueBASIC.h" int main(); void parameters(int *N, int *nmax, double *delta, double *period); void screen(int nmax, double period, int IC1_, int IC2_); void plotaxis(int nmax, double ticksize); void coefficents(int N, int nmax, double delta, double period, int IC1_, int IC2_); double f(double t); int main() { int N, nmax, IC1_ = 1, IC2_ = 2; double delta, period; GWopen(0); parameters(&N, &nmax, &delta, &period); screen(nmax, period, IC1_, IC2_); coefficents(N, nmax, delta, period, IC1_, IC2_); GWquit(); return 0; } void parameters(int *N, int *nmax, double *delta, double *period) { char Stmp1_[_LBUFF_]; printf("number of data points N (even) = "); fgets(Stmp1_, _LBUFF_, stdin); sscanf(Stmp1_, "%d", N); printf("sampling time dt = "); fgets(Stmp1_, _LBUFF_, stdin); sscanf(Stmp1_, "%lg", delta); (*period) = (*N)*(*delta); // 周期を仮定する // ナイキストの振動数に対応する基準振動の最大値 (*nmax) = (*N)/2; } void screen(int nmax, double period, int IC1_, int IC2_) { double ticksize, ymax; char Stmp_[_LBUFF_]; ymax = 2; ticksize = ymax/50; GWvport(0.0f, 0.5f, GWaspect(1), 1.0f); sprintf(Stmp_, " a_k frequency interval = %7.5f", 2*pi/period); GWindow(-1.0f, (float)(-ymax), (float)(nmax+1), (float)(ymax)); GWputtxt(0.0f, (float)(ymax*0.7), Stmp_); plotaxis(nmax, ticksize); GWsetpen(RED, -1, -1, -1); GWsavevp(IC1_); GWreset(); GWvport(0.0f, 0.0f, GWaspect(1), 0.5f); sprintf(Stmp_, " b_k"); GWindow(-1.0f, (float)(-ymax), (float)(nmax+1), (float)(ymax)); GWputtxt(0.0f, (float)(ymax*0.7), Stmp_); plotaxis(nmax, ticksize); GWsetpen(RED, -1, -1, -1); GWsavevp(IC2_); } void plotaxis(int nmax, double ticksize) { double k; GWline(0.0f, 0.0f, (float)(nmax), 0.0f); for(k = 1; k <= nmax; ++k) GWline((float)(k), (float)(-ticksize), (float)(k), (float)(ticksize)); } void coefficents(int N, int nmax, double delta, double period, int IC1_, int IC2_) { int i, k; double ak, bk, t, wk; for(k = 0; k <= nmax; ++k) { ak = 0; bk = 0; wk = 2*pi*k/period; // 長方形近似 for(i = 0; i <= N - 1; ++i) { t = i*delta; ak += f(t)*cos(wk*t); bk += f(t)*sin(wk*t); } ak *= 2.0/N; bk *= 2.0/N; GWselvp(IC1_); GWline((float)(k), 0.0f, (float)(k), (float)(ak)); GWselvp(IC2_); GWline((float)(k), 0.0f, (float)(k), (float)(bk)); } } double f(double t) { double f_, w0; w0 = 0.1*pi; f_ = sin(w0*t); // 簡単な例 return f_; }