| | 65 | - Code used for generating spectrograms and converting them into np 2D arrays: |
| | 66 | {{{#!python |
| | 67 | def plot_spect(dataset, title,min_value,max_value,normalized=False, save=None): |
| | 68 | if normalized: |
| | 69 | global_maximum = np.amax(np.abs(dataset)) |
| | 70 | else: |
| | 71 | global_maximum = 1 |
| | 72 | |
| | 73 | y_res = 20 * np.log10(np.abs(dataset) / global_maximum) |
| | 74 | |
| | 75 | fig = plt.figure(figsize=(12, 8)) |
| | 76 | im = plt.imshow(y_res, origin='lower', cmap='jet', aspect='auto', vmax=max_value, vmin=min_value) |
| | 77 | plt.colorbar(label='dB') |
| | 78 | |
| | 79 | yaxis = np.ceil(np.linspace(-15, 15, 11)) |
| | 80 | ylabel = np.linspace(0, len(y_res), 11) |
| | 81 | xaxis = np.linspace(0, .25, 5) |
| | 82 | xlabel = np.linspace(0, len(y_res[1]), 5) |
| | 83 | |
| | 84 | plt.title(title) |
| | 85 | plt.yticks(ylabel, yaxis) |
| | 86 | plt.xticks(xlabel, xaxis) |
| | 87 | plt.xlabel('Time') |
| | 88 | plt.ylabel('Frequency') |
| | 89 | plt.tight_layout() |
| | 90 | |
| | 91 | fig.canvas.draw() |
| | 92 | img_rgba = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8) |
| | 93 | img_rgba = img_rgba.reshape(fig.canvas.get_width_height()[::-1] + (4,)) |
| | 94 | img_rgb = img_rgba[:, :, [1, 2, 3]] |
| | 95 | |
| | 96 | return img_rgb |
| | 97 | }}} |