-
Notifications
You must be signed in to change notification settings - Fork 2
/
STBlockDemo.jl
150 lines (123 loc) · 3.62 KB
/
STBlockDemo.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
println("Hello World")
using SerialPorts
using PyPlot
using FFTW;
sp = SerialPort(list_serialports()[1], 9600) # port of the Teensy if connected
#create a chirp Pulse to be sent to the Teensy and plot
B=1715 #Bandwidth of the chirp signal
f=40000 #Center Frequency
T=6E-3 # Chirp Pulse length
K=B/T # Chirp rate
fs=2000000
dt=1/fs # The sampling rate was found to be 2Msamples/second
t_max=20/343 #max time to reach 10 meters and back is 58 miliseconds
t_d=T/2;
t_max_pulse=T;
t = collect(0:dt:t_max_pulse);
rect(t)=(abs.(t) .<=0.5)*1.0;
v_tx = UInt8.(round.((cos.(2*pi*(f*(t.-t_d).+0.5*K*(t.-t_d).^2)).*rect((t .-t_d)/T).+1).*127));
#println("Length of chirp pulse:",length(v_tx));
#Reading from the serial ADC clear the serial buffer
s=readavailable(sp) #clear the serial buffer
PyPlot.show()
while true
write(sp,'c') #to send and recieve something back
write(sp,v_tx)
while bytesavailable(sp)<1
continue
sleep(0.05)
end
s = readavailable(sp) #print the time for conversion
println("Reading and Transmitting...")
#Get the Values
write(sp,'p')
while bytesavailable(sp)<1
continue
end
a=""
i=0
while true
# global i
# global a
x=readavailable(sp)
if bytesavailable(sp)<1
sleep(0.05)
if bytesavailable(sp)<1
break
end
end
a=string(a,x)
end
adc=split(a,"\r\n")
len=length(adc)-1
v=Vector{Int64}(undef,len)
for i=1:len
# global v
# global adc
#v[i] = string(adc, base = 16)
v[i]=parse(Int64,adc[i])
end
#make another transmit signal at the same frequnecy as recieved
T=6E-3 # Chirp Pulse length
K=B/T # Chirp rate
fs=500000
dt=1/fs # The sampling rate was found to be 2Msamples/second
t_max=(20/343) +10E-3 # max time to reach 10 meters and back is 58 miliseconds
t_d=T/2;
t_max_pulse=T;
rect(t)=(abs.(t) .<=0.5)*1.0;
t_match=collect(0:dt:t_max);
v_tx_match = cos.(2*pi*(f*(t_match.-t_d).+0.5*K*(t_match.-t_d).^2)).*rect((t_match .-t_d)/T);
r=(343 .*t_match)/2
#recieved signal processing
v= (v/65535).-0.62
len2=length(r)-length(v)
b=zeros(len2)
append!(v,b)
len3=length(v)-length(v_tx_match)
c=zeros(len3)
append!(v_tx_match,c)
#println("The recieved and Transmitted ECHOES")
#Plot the two signals on one axis
#figure();
#plot(v);
#figure();
#println("Length of recieved signal:",length(v));
#plot(v_tx_match);
#println("Length of transmitted signal:",length(v_tx_match));
#println("The target location")
#matched filter signal Processing
V_TX=fft(v_tx_match);
V_RX=fft(v);
H = conj(V_TX);
# Apply Matched Filter to the simulated returns in Frequency Domain
V_MF = H.*V_RX;
v_mf = ifft(V_MF);
v_mf = real(v_mf);
#v_mf_range=v_mf .*rangeComp
#Plot the time domain outputs of matched filter
PyPlot.clf()
subplot(2,1,1)
PyPlot.plot(r,v_mf)
title("Matched filter output")
PyPlot.draw()
xlim([0,10]);
#Do analytical signal
V_ANAL= 2*V_MF; # make a copy and double the values
N = length(V_MF);
if mod(N,2)==0 # case N even
neg_freq_range = Int(N/2):N; # Define range of “neg-freq” components
else # case N odd
neg_freq_range = Int((N+1)/2):N;
end
V_ANAL[neg_freq_range] .= 0; # Zero out neg components in 2nd half of
v_anal = ifft(V_ANAL);
subplot(2,1,2)
PyPlot.plot(r,abs.(v_anal))
title("Analytical Signal")
xlabel("Range in meters");
PyPlot.draw()
PyPlot.sleep(0.05)
#sleep(0.05)
#close(sp)
end