/ concept-collection / windowedFourierProjection
Sign in
concept-collection / windowedFourierProjection
windowedFourierProjection / wfp_1Dspringscattering / utils / perispecdiff.m
25 lines · 853 BBlameHistoryRaw
1function g = perispecdiff(f)
2% PERISPECDIFF - use FFT to take periodic spectral differentiation of vector
3%
4% g = perispecdiff(f) returns g the derivative of the spectral interpolant
5% of f, which is assumed to be the values of a smooth 2pi-periodic function
6% at the N gridpoints 2.pi.j/N, for j=1,..,N (or any translation of such
7% points). Can be row or col vec, and output is same shape.
8%
9% Without arguments, does a self-test.
11% Barnett 2/18/14
12if nargin==0, test_perispecdiff; return; end
13N = numel(f);
14if mod(N,2)==0 % even
15 g = ifft(fft(f(:)).*[0 1i*(1:N/2-1) 0 1i*(-N/2+1:-1)].');
16else
17 g = ifft(fft(f(:)).*[0 1i*(1:(N-1)/2) 1i*((1-N)/2:-1)].');
18end
19g = reshape(g,size(f));
21%%%%%%
22function test_perispecdiff
23N = 50; tj = 2*pi/N*(1:N)';
24f = sin(3*tj); fp = 3*cos(3*tj); % trial periodic function & its deriv
25norm(fp-perispecdiff(f))
moveopenescclose