-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_real_vvt.m
78 lines (67 loc) · 2.31 KB
/
fft_real_vvt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function [out] = fft_real_vvt(x, inv)
%{
See fft_test.m for more information
fft_real_vvt returns the fourier transform of the signal on a set of normalized axes
- The length of the signal should be a power of 2.
- Out is forward FFT if inv = 0; the imag part is the projection onto
the -sin(2pi * f) axes. thus the -imag is the actual fourier coeficient.
- Out is the inverse fft if inv = 1;
- Out is the powerspectrum if inv = 2;
T = N*dt;
df = 1/T;
frequency = (k-1) * df; with k = (1:Nyquist_frequency_ind);
Nyquist_frequency = Nyquist_frequency_ind * df
Power is:
- pfx = fx .* conj(fx)
- Power is independent of dt
- Power has unities of x squared.
- sum(pfx) = sum(x.*x)
- sum(pfx) * dt = sum(x.*x)
- dt = T/N (energy)
Power spectral density
- pfx/df
- This makes the amplitude of the spectrum
independent of T or in turn of 0 padding.
The first value of fft_real_vvt(x) / sqrt(N) is mean(x)
%}
%% Reorient data to column vector
si = size(x);
if si(1) < si(2)
x = x';
end
%% Calculate Fourier transform
if inv == 0 || inv == 2
% Calculate normalization operator
N = size(x, 1);
Nyquist_frequency_ind = N/2 + 1;
normalization_operator = zeros(Nyquist_frequency_ind,1) + sqrt(2/N);
normalization_operator(1) = 1/sqrt(N);
normalization_operator(end) = 1/sqrt(N);
% Fourier transform
fx = fft(x);
out = fx(1:Nyquist_frequency_ind) .* normalization_operator;
a = 0;
% Power = x .* conj(x) = (abs(x))^2
if inv == 2
out = out .* conj(out); % pfx
end
elseif inv == 1
% Calculate normalization operator
Nyquist_frequency_ind = size(x,1);
N = (Nyquist_frequency_ind -1) * 2;
normalization_operator = zeros(Nyquist_frequency_ind,1) + sqrt(2/N);
normalization_operator(1) = 1/sqrt(N);
normalization_operator(end) = 1/sqrt(N);
x = x ./ normalization_operator;
x2 = flipud(x(2:Nyquist_frequency_ind -1));
x = [x;conj(x2)];
x(1) = real(x(1));
x(Nyquist_frequency_ind) = real(x(Nyquist_frequency_ind));
% Inverse Fourier transform
%{
A multiplication with a complec number e.g. when computing a time shift
causes the degenerated fourier coeficients to become complex.
They have to be set to real.
%}
out = ifft(x);
end