-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_functions.jl
152 lines (111 loc) · 4.89 KB
/
plot_functions.jl
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# plot function
function axis_object(fig,backbone,row,column)
ax = Axis(fig[row,column],backgroundcolor= :transparent,yticklabelcolor = backbone,
ytickcolor = backbone,xticklabelcolor = backbone,
xtickcolor = backbone, bottomspinecolor=backbone,topspinecolor=backbone,rightspinecolor=backbone,
leftspinecolor=backbone)
end
## update Points in a vector array of Point2f0
function update_points(x::Vector{T},y::Vector{R}) where {T,R}
point = @inbounds [Point2(i,j) for (i,j) in zip(x,y)]
end
function update_points(x::UnitRange{T},y::Vector{R}) where {T,R}
point = @inbounds [Point2(i,j) for (i,j) in zip(x,y)]
end
function update_points(x::Observable{UnitRange{T}},y::Observable{Vector{R}}) where {T,R}
point = update_points(x.val,y.val)
end
function update_points(x::Observable{StepRangeLen{T, Base.TwicePrecision{T}, Base.TwicePrecision{T}}},y::Observable{Vector{R}}) where {T,R}
point = update_points(x.val,y.val)
end
function update_points(x::StepRangeLen{T, Base.TwicePrecision{T}, Base.TwicePrecision{T}},y::Vector{R}) where {T,R}
point = @inbounds [Point2(i,j) for (i,j) in zip(x,y)]
end
#####
function plot_usv(sp,laser,air_down,time_down)
figure = Figure(resolution = (1000,600))
axes = [Axis(figure[i,1]) for i in 1:3]
sl_x = labelslider!(figure,"current position",1:1:25000,telheight = false,startvalue=3)
figure[4,1] = sl_x.layout
# air flow
x = 1 # as integer index
air_points = Observable(update_points(time_down[x:x+2000],air_down[x:x+2000]))
audio_points = Observable(update_points(sp.t[1:250*2000],sp.audio[1:250*2000]))
sng = Observable(sp.fout[:,x:x+4000]')
air_points = lift(sl_x.slider.value) do x
x_range = x:x+5000
ap = update_points(time_down[x_range],air_down[x_range])
xlims!(axes[1],time_down[x_range[1]],time_down[x_range[end]])
return ap
end
audio_points = lift(sl_x.slider.value) do x
y = 250*(x-1) +1
y_range = y:y+5000*250
ap2 = update_points(sp.t[y_range],sp.audio[y_range])
xlims!(axes[2],sp.t[y_range[1]],sp.t[y_range[end]])
return ap2
end
## Node for sng plots
sng = lift(sl_x.slider.value) do x
time_z = clamp(time_down[x],0,24.9)
sind = findfirst(x->x>=time_z,sp.t_sng)
sg = sp.fout[:,sind:sind+9765]'
# xlims!(axes[3],time_z,time_z+5)
return sg
end
lines!(axes[1],air_points,color=:red,linewidth=2)
lines!(axes[2],audio_points,color=:blue,linewidth=2)
heatmap!(axes[3],sng,colormap=:hot,colorrange=(0.0, 0.7))
figure
end
#####
# plot airflow, raw audio, and the spectrogram; specifies the timespan for visualization
function plot_usv(sp,laser,air_down,time_down,timespan::Int64)
figure = Figure(resolution = (1000,600))
axes = [Axis(figure[i,1]) for i in 1:3]
num = Int(sp.fs/1000)
sl_length = Int((sp.npts - sp.fs*timespan)/(num)) # 1000, 1ms resolution, timespan as second scale; pull back the limit
sl_x = labelslider!(figure,"current position",1:1:sl_length,telheight = false,startvalue=3)
figure[4,1] = sl_x.layout
# air flow
x = 1 # as integer index
# initialize
air_points = Observable(update_points(time_down[x:x+2000],air_down[x:x+2000]))
laser_points = Observable(update_points(time_down[x:x+2000],laser[x:x+2000]))
audio_points = Observable(update_points(sp.t[1:250*2000],sp.audio[1:250*2000]))
sng = Observable(sp.fout[:,x:x+4000]')
air_points = lift(sl_x.slider.value) do x
x_range = x:x+timespan*1000
ap = update_points(time_down[x_range],air_down[x_range])
xlims!(axes[1],time_down[x_range[1]],time_down[x_range[end]])
return ap
end
laser_points = lift(sl_x.slider.value) do x
x_range = x:x+timespan*1000
lp = update_points(time_down[x_range],laser[x_range].*4)
xlims!(axes[1],time_down[x_range[1]],time_down[x_range[end]])
return lp
end
audio_points = lift(sl_x.slider.value) do x
y = 250*(x-1) +1
y_range = y:y+timespan*1000*num
ap2 = update_points(sp.t[y_range],sp.audio[y_range])
xlims!(axes[2],sp.t[y_range[1]],sp.t[y_range[end]])
return ap2
end
time_limit = (sl_length/1000) - 0.004 # pull back 4ms to ensure the boundary
num_col = Int(floor(timespan/sp.dt_sng))
## Node for sng plots
sng = lift(sl_x.slider.value) do x
time_z = clamp(time_down[x],0,time_limit)
sind = findfirst(x->x>=time_z,sp.t_sng)
sg = sp.fout[:,sind:sind+num_col]'
# xlims!(axes[3],time_z,time_z+5)
return sg
end
lines!(axes[1],air_points,color=:red,linewidth=2)
lines!(axes[1],laser_points,color=:cyan,linewidth=1)
lines!(axes[2],audio_points,color=:blue,linewidth=2)
heatmap!(axes[3],sng,colormap=:hot,colorrange=(0.0, 0.7))
figure
end