Skip to content

Commit 50120f7

Browse files
authored
Merge pull request #12 from AUAS-Pulsar/filterbankplot
Filterbankplot
2 parents d20f783 + 4085d82 commit 50120f7

File tree

11 files changed

+215
-5
lines changed

11 files changed

+215
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
Creating a free and open source framework that contains the generic algorithms and file handling for astronomical data sets. This framework will be modular. Similar to OpenCV, wherein specific modules can be added and disabled depended on the needs of a project. This framework will be implemented in Python and C++.
99

10-
## Requirements
10+
11+
12+
13+
## Installation
14+
15+
### Requirements
1116

1217
* numpy
1318
* python 3.6
1419

20+
## Documentation
21+
You can find the documentation [here.](docs/README.md)
22+
1523
# Table of contents
1624
1. [Getting started](docs/gettingstarted.md)
1725
1. [Example filterbank files](docs/gettingstarted.md#11-example-filterbank-files)

data_import.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import filterbank.filterbank as fb
2+
3+
# Download the files in the README.md
4+
5+
def _8bit(freq_start, freq_stop):
6+
fb1 = fb.Filterbank(filename = "../data/pspm8.fil")
7+
8+
test1 = fb1.select_data(freq_start, freq_stop)
9+
10+
return test1
11+
12+
13+
def _16bit(freq_start, freq_stop):
14+
fb2 = fb.Filterbank(filename = '../data/pspm16.fil')
15+
16+
test2 = fb2.select_data(freq_start, freq_stop)
17+
18+
return test2
19+
20+
def _32bit(freq_start, freq_stop):
21+
fb3 = fb.Filterbank(filename = '../data/pspm32.fil')
22+
23+
test3 = fb3.select_data(freq_start, freq_stop)
24+
25+
return test3

docs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Documentation
2+
## Filterbank
3+
[Filterbank docs](filterbank.md)
4+
## Fourier transforms
5+
[Fourier transforms docs](fourier.md)
6+
## Plotting
7+
[Plotting docs](plots.md)

docs/fourier.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Fourier Transforms
2+
## DFT
3+
The `fourier.dft_slow` function is a plain implementation of discrete Fourier transformation.
4+
5+
The `fourier.fft_vectorized` function depends on this function.
6+
7+
### Parameters
8+
9+
| Parameter | Description |
10+
| --- | --- |
11+
| input_data | Array containing the values to be transformed. |
12+
13+
Returns an array containing the transformed values.
14+
15+
### Example usage:
16+
```python
17+
>>> from Asteria import fourier
18+
>>> fourier.dft_slow([1,2,3,4])
19+
array([10.+0.00000000e+00j, -2.+2.00000000e+00j, -2.-9.79717439e-16j, -2.-2.00000000e+00j])
20+
```
21+
22+
## FFT
23+
The `fourier.fft_vectorized` function is a vectorized, non-recursive version of the Cooley-Tukey FFT
24+
25+
Gives the same result as `fourier.dft_slow` but is many times faster.
26+
27+
### Parameters:
28+
29+
| Parameter | Description |
30+
| --- | --- |
31+
| input_data | Array containing the values to be transformed. |
32+
33+
Returns an array containing the transformed values.
34+
35+
### Example usage:
36+
```python
37+
>>> from Asteria import fourier
38+
>>> fourier.fft_vectorized([1,2,3,4])
39+
array([10.+0.00000000e+00j, -2.+2.00000000e+00j, -2.-9.79717439e-16j, -2.-2.00000000e+00j])
40+
```

docs/plots.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Plotting diagrams
2+
## PSD
3+
The `plot.psd` function can be used to generate the data for a Power Spectral Density plot.
4+
5+
### Parameters
6+
7+
| Parameter | Description |
8+
| --- | --- |
9+
| samples | 1-D array or sequence. Array or sequence containing the data to be plotted. |
10+
| nfft | Integer, optional. The number of bins to be used. Defaults to 256. |
11+
| sample_rate | Integer, optional. The sample rate of the input data in `samples`. Defaults to 2.|
12+
| window | Callable, optional. The window function to be used. Defaults to `plot.window_hanning`. |
13+
| sides | {'default', 'onesided', 'twosided'}. Specifies which sides of the spectrum to return. Default gives the default behavior, which returns one-sided for real data and both for complex data. 'onesided' forces the return of a one-sided spectrum, while 'twosided' forces two-sided. |
14+
15+
### Returns
16+
| Variable | Description |
17+
| --- | --- |
18+
| Pxx | 1-D array. The values for the power spectrum before scaling (real valued). |
19+
| freqs | 1-D array. The frequencies corresponding to the elements in `Pxx`. |
20+
21+
### Example Usage
22+
```python
23+
from filterbank.filterbank import Filterbank
24+
import matplotlib.pyplot as plt
25+
import numpy as np
26+
from plot import psd
27+
from filterbank.header import read_header
28+
29+
# Instatiate the filterbank reader and point to the filterbank file
30+
fb = Filterbank(filename='examples/pspm32.fil')
31+
32+
# read the data in the filterbank file
33+
_, samples = fb.select_data()
34+
35+
# Convert 2D Array to 1D Array with complex numbers
36+
samples = samples[0] + (samples[1] * 1j)
37+
38+
# Read the header of the filterbank file
39+
header = read_header('examples/pspm32.fil')
40+
41+
# Calculate the center frequency with the data in the header
42+
center_freq = header[b'fch1'] + float(header[b'nchans']) * header[b'foff'] / 2.0
43+
44+
print(center_freq)
45+
# Get the powerlevels and the frequencies
46+
PXX, freqs, _ = psd(samples, nfft=1024, sample_rate=80, sides='twosided')
47+
48+
# Calculate the powerlevel dB's
49+
power_levels = 10 * np.log10(PXX/(80))
50+
51+
# Add the center frequency to the frequencies so they match the actual frequencies
52+
freqs = freqs + center_freq
53+
54+
# Plot the PSD
55+
plt.grid(True)
56+
plt.xlabel('Frequency (MHz)')
57+
plt.ylabel('Intensity (dB)')
58+
plt.plot(freqs, power_levels)
59+
plt.show()
60+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Example of plotting a Power Spectral Density plot, using filterbank data
3+
"""
4+
# pylint: disable-all
5+
import os,sys,inspect
6+
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
7+
parentdir = os.path.dirname(currentdir)
8+
sys.path.insert(0,parentdir)
9+
from filterbank.filterbank import Filterbank
10+
import matplotlib.pyplot as plt
11+
import numpy as np
12+
from plot import psd
13+
from filterbank.header import read_header
14+
15+
# Instatiate the filterbank reader and point to the filterbank file
16+
fb = Filterbank(filename='examples/pspm32.fil')
17+
18+
# read the data in the filterbank file
19+
_, samples = fb.select_data()
20+
21+
# Convert 2D Array to 1D Array with complex numbers
22+
samples = samples[0] + (samples[1] * 1j)
23+
24+
# Read the header of the filterbank file
25+
header = read_header('examples/pspm32.fil')
26+
27+
# Calculate the center frequency with the data in the header
28+
center_freq = header[b'fch1'] + float(header[b'nchans']) * header[b'foff'] / 2.0
29+
30+
print(center_freq)
31+
# Get the powerlevels and the frequencies
32+
PXX, freqs = psd(samples, nfft=1024, sample_rate=80,
33+
scale_by_freq=True, sides='twosided')
34+
35+
# Calculate the powerlevel dB's
36+
power_levels = 10 * np.log10(PXX/(80))
37+
38+
# Add the center frequency to the frequencies so they match the actual frequencies
39+
freqs = freqs + center_freq
40+
41+
# Plot the PSD
42+
plt.grid(True)
43+
plt.xlabel('Frequency (MHz)')
44+
plt.ylabel('Intensity (dB)')
45+
plt.plot(freqs, power_levels)
46+
plt.show()

examples/visualize_psd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
# Close RTLSDR device connection
2727
SDR.close()
2828

29+
print(SAMPLES[1:100])
2930
# Number of samples equals the length of samples
3031
N = SAMPLES.shape[0]
3132

3233
# T equals N/Fs
3334
T = N/SDR.sample_rate
3435

3536
# Get the powerlevels and the frequencies
36-
PXX, freqs, _ = psd(SAMPLES, nfft=1024, sample_rate=SDR.sample_rate/1e6, # pylint: disable-msg=C0103
37+
PXX, freqs = psd(SAMPLES, nfft=1024, sample_rate=SDR.sample_rate/1e6, # pylint: disable-msg=C0103
3738
scale_by_freq=True, sides='twosided')
3839

3940
# Calculate the powerlevel dB's

filterbank/__init__.pyc

109 Bytes
Binary file not shown.

filterbank/filterbank.pyc

4.4 KB
Binary file not shown.

fourier/fourier.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77

88
def dft_slow(input_data):
99
"""
10-
Compute the discrete Fourier Transform of the one-dimensional array x
10+
Compute the discrete Fourier Transform of the one-dimensional array input_data
11+
12+
Parameters
13+
----------
14+
input_data : ndarray
15+
Array of length `n` containing the values to be transformed.
16+
17+
Returns
18+
-------
19+
ndarray
20+
Array of length `n` containing the transformed values.
1121
"""
1222
input_data = np.asarray(input_data)
1323
data_length = input_data.shape[0]
@@ -20,6 +30,16 @@ def dft_slow(input_data):
2030
def fft_vectorized(input_data):
2131
"""
2232
A vectorized, non-recursive version of the Cooley-Tukey FFT
33+
34+
Parameters
35+
----------
36+
input_data : ndarray
37+
Array of length `n` containing the values to be transformed.
38+
39+
Returns
40+
-------
41+
ndarray
42+
Array of length `n` containing the transformed values.
2343
"""
2444
input_data = np.asarray(input_data)
2545
data_length = input_data.shape[0]

0 commit comments

Comments
 (0)