y=1(0≤t<π)
y=-1(-π≤t<0)
y(t+2π)=y(t)
の周期関数(周期2π。原点で不連続)とする。
固有周波数f0=1/T。
以下のようにフーリエ級数展開するとき、
係数は以下のように計算される。
clear; close all; clc; T=2*pi; f0=1/T; dt=0.01; t=-T/2:dt:T/2; y=f(t); plot(t,y,'r','DisplayName','original'); hold on; a0=0; a=@(n) 0; b=@(n) 2*(1-(-1)^n)/(pi*n); % nmax=10; ns=[1,3,5,99]; for idx=1:length(ns) nmax = ns(idx); yt=fourier_series(t,f0,a0,a,b,nmax); plot(t,yt,'DisplayName',sprintf('N=%d',nmax)); end legend(); big; function res=f(t) res = []; for idx=1:length(t) ti=t(idx); if ti<0 res(idx)=-1; else res(idx)=1; end end end function ft=fourier_series(t,f0,a0,a,b,nmax) ft = a0/2; for n=1:nmax an = a(n); bn = b(n); ft = ft + an*cos(2*pi*f0*n*t) + bn*sin(2*pi*f0*n*t); end end