-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_noa_freq.py
195 lines (165 loc) · 5.69 KB
/
find_noa_freq.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: FG Find NOA frequency
# Generated: Wed May 1 16:24:25 2019
##################################################
import os
import sys
sys.path.append(os.environ.get('GRC_HIER_PATH', os.path.expanduser('~/.grc_gnuradio')))
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from thread import start_new_thread
from concurrent.futures import ProcessPoolExecutor,ALL_COMPLETED
from optparse import OptionParser
import datetime
import time
import concurrent
import numpy as np
IQ_FILE = r"d:\Program Files (x86)\SDRSharp\NOAA15\SDRSharp_20190112_173333Z_138063526Hz_IQ.wav"
samp_rate = 2048000
BW = samp_rate
Relative_Center_Frequency = BW/2
NOT_FOUND = True
FOUND_IN_ROW = 0
FIR_NEED = 6
LAST_XFREQ = 0
THRESHOLD = 1.5
THRESHOLD2 = 0.5
THRESHOLD3 = 0.1
GAP_WIDTH = 4800
SIG_WIDTH = 200
pos = 0.0
def upd_samp_rate(sr):
global samp_rate
samp_rate = sr
BW = samp_rate
def max_sig(X,i,n,v):
startIdx = i+(n*2+v)*GAP_WIDTH
m = max(X[startIdx:startIdx+SIG_WIDTH])
return m
def find_xfreq(in00,lpos):
global samp_rate,THRESHOLD,THRESHOLD2,THRESHOLD3
global GAP_WIDTH,NOT_FOUND,FOUND_IN_ROW
global LAST_XFREQ
global Relative_Center_Frequency,BW
rpos = 0
X = np.abs(np.fft.fft(in00))
if(os.path.isfile("snapshot.txt")):
return True
print("%s: %06.2f s [%09d] | len(X):%d" % (datetime.datetime.now(),lpos/samp_rate,lpos,len(X)))
found = False
for i in range(0,len(X)-14*GAP_WIDTH):
rpos += 1
tcheck1 = True
for n in range(0,4):
tcheck1 = tcheck1 and (max_sig(X,i,n,0)>max_sig(X,i,n,1)*THRESHOLD)
for n in range(0,4):
tcheck1 = tcheck1 and (max_sig(X,i,n,1)*THRESHOLD<max_sig(X,i,n,2))
if tcheck1:
found = True
xfreq = i*BW/samp_rate-Relative_Center_Frequency
if FOUND_IN_ROW != 0:
if abs(LAST_XFREQ - xfreq)< (xfreq*THRESHOLD3):
FOUND_IN_ROW = 0
LAST_XFREQ = xfreq
FOUND_IN_ROW += 1
if FOUND_IN_ROW == FIR_NEED:
if(os.path.isfile("snapshot.txt")):
return True
print("\nFound xfreq: %d at %06.2f s [%09d | i:%d], you can now stop." % (xfreq,lpos/samp_rate,lpos+rpos,i))
for n in range(-16,16):
print("%d: %f,%f" % (n,max_sig(X,i,n,0),max_sig(X,i,n,1)))
f = open("snapshot.txt","w+")
d = 0
for x in X:
f.write("%d,%f\n" % (d,x))
d += 1
f.close()
NOT_FOUND = False
break
else:
FOUND_IN_ROW = 0
print("%s ." % datetime.datetime.now())
if not found:
FOUND_IN_ROW = 0
return not NOT_FOUND
class find_noa_freq_out(gr.basic_block):
executor = ProcessPoolExecutor(max_workers=6)
all_futures = []
def __init__(self):
global samp_rate ,NOT_FOUND, Relative_Center_Frequency
gr.basic_block.__init__(
self,
name="OUT Find NOA frequency",
in_sig = [(np.float32,samp_rate)],
out_sig = None
)
try:
os.remove("snapshot.txt")
except:
pass
def general_work(self, input_items, output_items):
global samp_rate,THRESHOLD,THRESHOLD2,THRESHOLD3
global GAP_WIDTH,NOT_FOUND,FOUND_IN_ROW
global LAST_XFREQ
global Relative_Center_Frequency,BW
global pos
in0 = input_items[0]
in00 = in0[0]
l = len(in0)
l00 = len(in00)
lo = len(output_items)
if NOT_FOUND:
ndcnt = 0
for f in self.all_futures:
if not f.done():
ndcnt += 1
elif f.result():
NOT_FOUND = False
self.consume(0, l)
self.produce(0, lo)
return 0
if(os.path.isfile("snapshot.txt")):
self.consume(0, l)
self.produce(0, lo)
return 0
if ndcnt>=8:
concurrent.futures.wait(self.all_futures,return_when=ALL_COMPLETED)
a = self.executor.submit(find_xfreq,in00,pos)
self.all_futures.append(a)
pos += samp_rate
if a is None:
raise Exception("FAIL")
self.consume(0, l)
self.produce(0, lo)
return 0
class find_noa_freq(gr.top_block):
def __init__(self):
global samp_rate,IQ_FILE
gr.top_block.__init__(self, "TOP Find NOA frequency")
##################################################
# Blocks
##################################################
self.blocks_wavfile_source_0_0 = blocks.wavfile_source(IQ_FILE, False)
upd_samp_rate(self.blocks_wavfile_source_0_0.sample_rate())
self.blocks_stream_to_vector = blocks.stream_to_vector(gr.sizeof_float,samp_rate)
self.out = find_noa_freq_out()
self.connect((self.blocks_wavfile_source_0_0, 0), (self.blocks_stream_to_vector, 0))
self.connect((self.blocks_stream_to_vector, 0), (self.out, 0))
print("sample_rate: %d" % samp_rate)
def main(top_block_cls=find_noa_freq, options=None):
tb = top_block_cls()
tb.start()
try:
raw_input('Press Enter to quit: \n')
except EOFError:
pass
tb.stop()
tb.wait()
if __name__ == '__main__':
main()