This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsinad.py
85 lines (68 loc) · 2.74 KB
/
sinad.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
#!/usr/bin/env python2
#
# Copyright 2013 <+YOU OR YOUR COMPANY+>.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import numpy as np
from gnuradio import gr
class sinad_ff(gr.sync_block):
"""
docstring for block sinad_ff
"""
def __init__(self, sinadFreq, Fs):
gr.sync_block.__init__(self, "sinad_ff", [np.float32], [np.float32])
self.Fs = Fs
self.fRef = sinadFreq
self.refWidth = 20
def work(self, input_items, output_items):
output_items[0][:] = self.__calc_sinad(input_items[0])
return len(output_items[0])
def __calc_sinad(self, data):
""" Takes float array and returns the sinad in dB
"""
# %% compute FFT
# 5/9/07 - added data windowing - ASP
window = np.hamming(np.size(data))
for n in range(np.size(data)):
data[n] = data[n] * window[n]
psd = np.fft.fft(data)
psd = psd.flatten()
# %% determine bin indices
bin1 = (
int(np.floor(float((self.fRef - self.refWidth / 2)) / float(self.Fs) * psd.shape[0]))
- 1
)
bin2 = (
int(np.ceil(float((self.fRef + self.refWidth / 2)) / float(self.Fs) * psd.shape[0]))
- 1
)
# 4/9/07 - added filtering 300 - 3000Hz ASP
bin300hz = int(np.floor(float(300) / float(self.Fs) * psd.shape[0])) - 1
bin3000hz = int(np.floor(float(3000) / float(self.Fs) * psd.shape[0])) - 1
# %% calculate SINAD = 10*log10(Ps/Pn)
# 4/9/07 change signal = psd[bin1:bin2] to psd[bin300hz:bin3000hz] - ASP
signal = psd[bin300hz:bin3000hz]
noise = np.concatenate((psd[bin300hz:bin1], psd[(bin2 + 1):bin3000hz]))
# Ps1 = float(sum(signal.real*signal.real + signal.imag*signal.imag)) #15/8 DON
# Pn1 = float(sum(noise.real*noise.real + noise.imag*noise.imag)) #15/8 DON
Ps = np.sum(np.abs(signal) ** 2)
Pn = np.sum(np.abs(noise) ** 2)
sinad = 10 * np.log10(Ps / Pn)
# if the tone is not present, sinad will be < 0
if sinad < 0:
sinad = 0
return sinad