-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstft.py
More file actions
86 lines (75 loc) · 2.43 KB
/
stft.py
File metadata and controls
86 lines (75 loc) · 2.43 KB
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
78
79
80
81
82
83
84
85
86
from __future__ import division
from numpy import zeros,array,arange,linspace,pi,sin,cos,mean,loadtxt
from numpy.fft import fft
import matplotlib.pyplot as plt
#Hann window function
def hann(t,width):
t += width/2
if 0<=t<=width:
return 0.5 * (1-cos(2*pi*t/(width)))
else:
return 0
#Short-Time Fourier Transform
def stft(signal,dt,windowlength):
N = len(signal)
T = N*dt
times = arange(0,T,dt)
coefficients = zeros((N, N//2))
for i in range(N):
window = array([hann(t-i*dt, windowlength) for t in times])
multiplied = signal*window
ft = fft(multiplied)[:N//2]
coefficients[i] = abs(ft)
return coefficients
if __name__ == '__main__':
#Test signal
testtimes = linspace(0,10,1024)
testsignal= array([sin(30*x) for x in testtimes])
testsignal[512:] = array([sin(70*x) for x in testtimes[512:]])
dt = 10/1024
#fMRI data
signal1 = loadtxt('subj001_DMN_PCC.txt')
signal2 = loadtxt('subj001_ECN_LDLPFC.txt')
signal1 -= mean(signal1)
signal2 -= mean(signal2)
#Sampling rate of the fMRI data is 2.5s
dt1 = 2.5
#Compute STFTs for the three signals
image1 = stft(testsignal,dt,2)
image2 = stft(signal1,dt1,30)
image3 = stft(signal2,dt1,30)
#Plot results
plt.figure(1)
plt.subplot(211)
plt.plot(testtimes,testsignal)
plt.xlim((0,dt*len(testsignal)))
plt.subplot(212)
plt.imshow(image1.T,aspect='auto',origin='lower',
extent=[0,20,0,len(testsignal)//2])
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.title('STFT, window width = 2 s')
plt.figure(2)
plt.subplot(211)
plt.plot(arange(0,dt1*len(signal1),dt1),signal1)
plt.xlim(0,dt1*len(signal1))
plt.title('Default mode network')
plt.grid()
plt.subplot(212)
plt.imshow(image2.T,aspect='auto',origin='lower',
extent=[0,dt1*len(signal2),0,len(signal1)//2])
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.title('STFT, window width = 30 s')
plt.figure(3)
plt.subplot(211)
plt.plot(arange(0,dt1*len(signal2),dt1),signal2)
plt.xlim(0,dt1*len(signal2))
plt.title('Executive control network')
plt.grid()
plt.subplot(212)
plt.imshow(image3.T,aspect='auto',origin='lower',
extent=[0,dt1*len(signal1),0,len(signal2)//2])
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.title('STFT, window width = 30 s')