Advertisement
Guest User

FM SCanner

a guest
Jan 22nd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. import sys
  2. from pathlib import Path
  3. import time
  4. from threading import Thread
  5. import datetime as dt
  6. import numpy as np
  7. from sklearn.decomposition import PCA
  8. from sklearn.preprocessing import StandardScaler
  9. from RSA_API import RSAError
  10. from keras.models import load_model
  11.  
  12. from rsa_config import RSAConfig
  13.  
  14. import dpx_data_file_management as dpx_file
  15. from dpx_graph_data_stream import create_dpx_data_stream
  16.  
  17. class FM_Scanner:
  18.     def __init__(self):
  19.         self.fm_list = []
  20.        
  21.         self.model = load_model('.\MachineLearningScripts\CNN\cnn.h5')
  22.  
  23.         self.config_cf = 8e7 # centerfrequency starts at 80MHz
  24.     #    = RSAConfig(cf=8e7) #8- MHz
  25.         self.stream = create_dpx_data_stream(self.config)
  26.        
  27.         self.loop_fm_range()
  28.     def loop_fm_range():
  29.        
  30.         #Open stream
  31.         try:
  32.             self.stream.open()
  33.         except (RSAError, ValueError) as err:
  34.             print("Could not connect to RSA", str(err))
  35.             return
  36.            
  37.        
  38.         while (self.config_cf <= 12e7): # while centerfrequency less than 120MHz
  39.             #Grab data
  40.             data = self.stream.get_previous_dpx_data()
  41.            
  42.             bitmap = np.asarray([data.DPX_bitmap])
  43.            
  44.            
  45.             #Data preprocess -> ONLY FOR CNN MODEL!
  46.             sc = StandardScaler()
  47.             tmp = sc.fit_transform(bitmap[0])
  48.             pca = PCA(n_components=32) #TODO: Parametrize?
  49.             = pca.fit_transform(tmp)
  50.             # End of data preprocessing
  51.                        
  52.                        
  53.             #Reshape data to fit the input_shape for the model-> Keep first 3 dimensions the same (batch_size, width, height). The last dimension is the depth (1 = BW, 3 = RGB...)
  54.             arr = []
  55.             arr.append(tmp)
  56.             arr = np.asarray(arr)
  57.             arr = arr.reshape(arr.shape[0],201,32,1)
  58.             prediction = self.model.predict(arr, verbose=0)
  59.             if (prediction[0][0] >= .99):
  60.                 self.fm_list.append(self.config_cf)
  61.             #Increase frequency by 0.1 MHz each iteration
  62.             self.config_cfg = self.config_cf + 1e5
  63.             self.stream.set_rsa_config(cf = self.config_cfg)
  64.         self.stream.close()
  65.         print(self.fm_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement