フーリエ級数その2(不連続関数)

y=1(0≤t<π)
y=-1(-π≤t<0)
y(t+2π)=y(t)
の周期関数(周期2π。原点で不連続)とする。
有周波数f0=1/T。
以下のようにフーリエ級数展開するとき、
 y=\dfrac{a0}{2}+\displaystyle\sum_{n=1}^{\infty} \cos(2 \pi f_0 n)t+\sin (2 \pi f_0 n) t
係数は以下のように計算される。
 a_0=0
 a_n=0
 b_n=\dfrac{2(1-(-1)^n)}{\pi n}

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

f:id:seinzumtode:20210613173935p:plain