-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsurrogates.m
64 lines (53 loc) · 1.43 KB
/
surrogates.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
%output = surrogates(x) returns a
%phase-randomized copy of x
function output = surrogates(x)
%length of x
N = length(x);
%apply DFT
s = fft(x);
%plot(abs(s),'g'), length(s)
%construct (conjugate symmetric) array of random phases
phase_rnd = zeros(1,N);
%define first phase
phase_rnd(1) = 0;
if (odd(N) == 1);
ph = 2*pi.*rand(1,(N-1)/2) - pi;
phase_rnd(2:N) = [ph,-flipdim(ph,2)];
end
if (odd(N) == 0);
ph(1:(N-2)/2) = 2*pi.*rand(1,(N-2)/2) - pi;
phase_rnd(2:N) = [ph,0,-flipdim(ph,2)];
end
%randomize phases
s_rnd = zeros(1,N); %initialization
s_rnd = abs(s).*exp(i.*(angle(s) + phase_rnd));
s_rnd(1) = s(1); %to make sure that s_rnd(1) is real
%apply inverse DFT
x_rnd = ifft(s_rnd,'symmetric'); %use "symmetric" because of round-off errors
%define output
output = x_rnd;
%---------------------------------------------------------
% o = odd(n) equals 1 if n is odd, 0 if n is even
function outp = odd(n);
for i = 1:round(n/2);
if (2*i == n);
outp = 0;
else
outp = 1;
end
end
if (n == 0);
outp = 0;
end
end
%---------------------------------------------------------
%plots
% subplot(2,2,1);
% plot(x,'k'), title('ORIGINAL SIGNAL');
% subplot(2,2,2);
% plot(x_rnd,'k'), title('PHASE RANDOMIZED SIGNAL');
% subplot(2,2,3);
% plot(angle(s)), title('ORIGINAL PHASES');
% subplot(2,2,4);
% plot(angle(s_rnd)), title('RANDOMIZED PHASES');
end