|
| 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 | +``` |
0 commit comments