nperseg=100) print(t.shape, f.shape, Zxx.shape) # print(f.shape, f[:10]) # print(t.shape, t[:10]) # print(Zxx.shape, Zxx[:10]) # print(len([stft**2 for stft in Zxx]), [stft**2 for stft in Zxx][:10]) signal_fig = FigData( xs=nst.samp_nums, ys=[nst.data], title=f"(a) Non stationary signal (samp rate = {samp_rate})", plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") stft_fig = apreset.stft_pcolormesh(t=t, f=f, Zxx=Zxx, title=f"(b) STFT Magnitude", yscale="linear", ylim=(0, 50), colorbar_params={"pad": 0.05}) aplot.single_subplots(grid_size=(1, 2), fig_data={ (0, 0): signal_fig, (0, 1): stft_fig }, individual_figsize=(5, 3.5), savefig_path="stft_sines.png")
sine_fft_fig = FigData(xs=sine_fft_x[:sine_slice_num], ys=np.abs(sine_fft[:sine_slice_num]), title=sine_sum_fft_title, options=["grid"], **fft_axis_labels) # # Plot # aplot.single_subplots(grid_size=(3, 1), fig_data={ (0, 0): cl_raw_fig, (1, 0): cl_signal_fig, (2, 0): cl_fft_fig }, individual_figsize=(8, 2.8), dpi=300, savefig_path="Clarinet_FFT") aplot.single_subplots( grid_size=(3, 1), fig_data={ (0, 0): sine_raw_fig, (1, 0): sine_signal_fig, (2, 0): sine_fft_fig }, individual_figsize=(12, 3), savefig_path=f"{str(sine_signal.sin_freqs).replace(' ', '')}Hz_Sine_Sum_FFT" )
# Create figures figs[(row, col)] = FigData(xs=np.array(range(Nx)), ys=[window_data[window_name]], title=window) figs_response[(row, col)] = FigData(xs=np.array(freq), ys=[response], title=window, xlim=(-0.5, 0.5), ylim=(-120, 0)) # # Plot window data # aplot.single_subplots(grid_size=(nrows, ncols), fig_data=figs, individual_figsize=(3.5, 1.6), title="All Scipy Supported Windows", xlabel="Sample", ylabel="Amplitude", savefig_path="scipy_windows") aplot.single_subplots(grid_size=(nrows, ncols), fig_data=figs_response, individual_figsize=(3.5, 1.6), title="Frequency Response of each window", xlabel="Normalized frequency (cycles per sample)", ylabel="Normalized magnitude (dB)", savefig_path="scipy_windows_freq_responses")
recon_fig = FigData(xs=trec, ys=xrec, title="Reconstructed after filtering:\n" f"{scale_name} minor triads w/ inversions", plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") # # Display results # aplot.single_subplots( grid_size=(2, 2), fig_data={ (0, 1): stft_fig, (1, 1): stftf_fig, (0, 0): signal_fig, (1, 0): recon_fig }, individual_figsize=(6, 4), savefig_path= f"ISTFT_{window_name}_{window_length}_{int(overlap_percent*100)}%_Pure_{scale_name}_minor_chord_sampr={samp_rate}" ) wav.write( f"audio/ISTFT_{window_name}_{window_length}_{int(overlap_percent * 100)}%_Pure_{scale_name}m", np.concatenate((nst.data, xrec)), samp_rate=samp_rate)
recon_stft_figs = [None for i in range(len(recon_stfts))] for i, recon_stft in enumerate(recon_stfts): recon_stft_figs[i] = apreset.stft_pcolormesh( t=t, f=f, Zxx=recon_stft, title= f"Separated source '{sep_names[i]}'\nat d={ds[i]} with width H={Hs[i]}", yscale="linear", ylim=ylim) fig_data[(2, i + 1)] = recon_stft_figs[i] # Plot null/peaks, src & source STFTs aplot.single_subplots( grid_size=(3, 4), fig_data=fig_data, individual_figsize=(6, 4), # savefig_path=f"ADRess_{channel}_{tech_name}.png" ) # # Output audio # wav.write(f"audio/{name}_stereo", src.T, samp_rate=samp_rate) wav.write(f"audio/{name}_left", left_signal, samp_rate=samp_rate) wav.write(f"audio/{name}_right", right_signal, samp_rate=samp_rate) for i, left_recon in enumerate(left_recons): wav.write(f"audio/{tech_name}_{short_sep_names[i]}", left_recon, samp_rate=samp_rate)
fft_figs = [FigData(xs=[np.linspace(0, max_response_freq, 1024), x[:psns[i]+1]], ys=[zp_responses[i][:1024], responses[i][:psns[i]+1]], title=f"({fig_labels[i * 2 + 1]}) Magnitude of DFT ({freq * signals[i].duration} cycles)", options=["grid", "xres"], line_options=fft_line_options, **fft_axis_labels) for i, x in enumerate(freqs)] ws_fft_fig = FigData(xs=[np.linspace(0, max_response_freq, 1024), freqs[1][:psns[1]+1]], ys=[wszp_response[:1024], ws_response[:psns[1]+1]], title=f"(f) Magnitude of DFT (Windowed, {freq * signals[1].duration} cycles)", options=["grid", "xres"], line_options=fft_line_options, **fft_axis_labels) # # Plot # aplot.single_subplots(grid_size=(3, 2), fig_data={(0, 0): sig_figs[0], (0, 1): fft_figs[0], (1, 0): sig_figs[1], (1, 1): fft_figs[1], (2, 0): ws_fig, (2, 1): ws_fft_fig}, individual_figsize=(5.5, 3), title=f"{freq}Hz Sinusoid sampled at {samp_rate}Hz", savefig_path="Spectral_Leakage" )
ys=[sig.data], title=f"({chr(label)}) {name} of {note_names}", plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") label += 2 fft_figs[i] = FigData(xs=fft_x[:slice_num], ys=np.abs(ft) ** 2, title=f"({chr(label)}) Fourier Transform Magnitude of {name}", options=["grid"], xlabel="Frequency (Hz)", ylabel="Normalised Magnitude") label -= 1 # Plot aplot.single_subplots(grid_size=(2, 2), fig_data={(0, 0): sig_figs[0], (0, 1): sig_figs[1], (1, 0): fft_figs[0], (1, 1): fft_figs[1]}, individual_figsize=(4, 2.5), auto_timestamp=True, savefig_path=f"{underline_note_names}_{round(duration*1000)}ms_{name}_FFT.png" ) # Output wav # wav.write(f"audio/{underline_note_names}_{round(duration*1000)}ms_melody", melody) # wav.write(f"audio/{underline_note_names}_{round(duration*1000)}ms_chord", chord)
plot_type="plot", xlabel="Time (s)", ylabel="Amplitude") fft_fig = FigData(xs=fft_x[:slice_num], ys=np.abs(ws_ffts[seg])**2, title="Fourier Transform Magnitude", options=["grid"], ylim=(-0.025, 0.25), xlabel="Frequency (Hz)", ylabel="Normalised Amplitude") aplot.single_subplots(grid_size=(2, 1), fig_data={(0, 0): ws_fig, (1, 0): fft_fig}, individual_figsize=(5, 2.5), auto_timestamp=False, folder="stft_schematic", savefig_path=f"tau={tau}s.png" ) # Plot the 3D vis of the FFTs which forms the STFT x, y = np.meshgrid(fft_x, np.arange(0, n_segments) / sum(durations)) fft_stack_fig = FigData(xs=x, ys=y, zs=[np.abs(np.array(ws_ffts))**2], title="STFT surface from stacking FFT", plot_type="plot_surface", xlabel="Frequency (Hz)", ylabel="Time (s)", zlabel="Normalised Amplitude", line_options=[{"cmap": "viridis"}],
r_stft_rec_fig = apreset.stft_pcolormesh(t=t, f=f, Zxx=right_stft_recon, title=f"Separated source at d={right_d} with width H={right_H}", yscale="linear", ylim=r_ylim) # # Output # # Plot the frequency-azimuth spectrogram at the specified time frame aplot.single_subplots(grid_size=(2, 2), fig_data={(0, 0): l_azi_null_fig, (1, 0): l_azi_peak_fig, (0, 1): l_stft_src_fig, (1, 1): l_stft_rec_fig, }, individual_figsize=(6, 4), savefig_path=f"ADRess_left_{tech_name}.png") aplot.single_subplots(grid_size=(2, 2), fig_data={(0, 0): r_azi_null_fig, (1, 0): r_azi_peak_fig, (0, 1): r_stft_src_fig, (1, 1): r_stft_rec_fig, }, individual_figsize=(6, 4), savefig_path=f"ADRess_right_{tech_name}.png") # Output audio file for i, chord in enumerate(chord_names):
"label": window.__name__ }], **signal_axis_labels) cl_fft_fig = FigData( xs=cl_fft_x[:cl_slice_num], ys=[np.abs(cl_fft[:cl_slice_num]), np.abs(cl_window_fft[:cl_slice_num])], title=cl_fft_title, line_options=[{ "label": cl_signal_title }, { "label": cl_window_title }], options=["grid"], plot_type="semilogy", **fft_axis_labels) # # Plot # aplot.single_subplots(grid_size=(2, 2), fig_data={ (0, 0): cl_signal_fig, (0, 1): cl_window_fig, (1, 0, 1, 2): cl_fft_fig }, individual_figsize=(6, 4), savefig_path=f"Clarinet_FFT_Full_{window.__name__}")
f"STFT Magnitude (Zoomed) of {scale_name} Major scale\n(Artificial clarinet sound)", line_options=[{ "vmin": 0, "vmax": Zxx_max, "shading": 'gouraud' }], options=["grid"], plot_type="pcolormesh", xlabel="Time (s)", ylabel="Frequency (Hz)", fit_data=False) # # Display results # aplot.single_plot(fig_data=signal_fig) aplot.single_subplots( grid_size=(2, 2), fig_data={ (0, 0): fft_fig, (1, 0): fft_zoomed_fig, (0, 1): stft_fig, (1, 1): stft_zoomed_fig }, individual_figsize=(6, 4), savefig_path=f"STFT_Clarinet_{scale_name}_Major_scale_sampr={samp_rate}") # wav.write(f"audio/cl_{scale_name}_scale", nst)
window=window, nperseg=nperseg, noverlap=noverlap) true_pos_stft_fig = apreset.stft_pcolormesh(t=t, f=f, Zxx=stft, title=f"True signal at stereo position {pos}", yscale="linear", ylim=global_ylim, colorbar_params={}) fig_data[(4, i)] = true_pos_stft_fig # Plot L/R STFTs, null/peaks, recon STFTs & true STFTs at each stereo pos aplot.single_subplots(grid_size=(5, 4), fig_data=fig_data, individual_figsize=(4.8, 3), savefig_path=f"StereoADRess_{tech_name}.png", show=False ) aplot.single_subplots(grid_size=(4, 2), fig_data={(0, 0): fig_data[(0, 0)], (0, 1): fig_data[(0, 1)]}, individual_figsize=(4.8, 3), savefig_path=f"ADRess_{tech_name}_LR.png", show=False ) aplot.single_subplots(grid_size=(4, 2), fig_data={(0, 0): fig_data[(1, 0)], (0, 1): fig_data[(2, 0)], (1, 0): fig_data[(1, 1)],
nperseg=nperseg, noverlap=noverlap) true_pos_stft_fig = apreset.stft_pcolormesh( t=t, f=f, Zxx=stft, title=f"True signal at stereo position {pos}", yscale="linear", ylim=global_ylim, max_mag=vmax) fig_data[(4, i)] = true_pos_stft_fig # Plot L/R STFTs, null/peaks, recon STFTs & true STFTs at each stereo pos aplot.single_subplots(grid_size=(5, 4), fig_data=fig_data, individual_figsize=(6, 4), savefig_path=f"FitzADRess_{tech_name}.png", show=False) # # Output audio # wav.write(f"audio/{name}_stereo", src.T, samp_rate=samp_rate) wav.write(f"audio/{name}_left", left_signal, samp_rate=samp_rate) wav.write(f"audio/{name}_right", right_signal, samp_rate=samp_rate) # True audio at each stereo position for pos, audio in true_signals.items(): wav.write(f"audio/{name}_pos={pos}", audio, samp_rate=samp_rate)
null_fig = FigData(xs=np.arange(beta + 1), ys=f, zs=nulls[i], title=f"Frequency-azimuth spectrogram\n(tau={'{:.2f}'.format(tau)}s)", line_options=azi_line_options, ylim=(0, 4000), **azi_fig_params) azi_line_options[0]["vmax"] = np.max(peaks[i]) * 0.25 peak_fig = FigData(xs=np.arange(beta + 1), ys=f, zs=peaks[i], title=f"Null magnitude estimation (tau={'{:.2f}'.format(tau)}s)", line_options=azi_line_options, ylim=(0, 4000), **azi_fig_params) fig_data[(0, i)] = null_fig fig_data[(1, i)] = peak_fig # Plot null/peaks aplot.single_subplots(grid_size=(2, len(taus)), fig_data=fig_data, individual_figsize=(6, 4), folder=f"URMP_Analysis/{idx}", savefig_path=f"{taus}.png".replace(" ", ""), auto_timestamp=False, show=False )