def test_translate_syn_spectrum(): syn_params = SynParams([1, 1], [10, 0.5, 1], 0) freqs, spectrum = gen_power_spectrum([3, 40], *syn_params) translated_spectrum, new_syn_params = translate_syn_spectrum( spectrum, 0.5, syn_params) assert not np.all(translated_spectrum == spectrum) assert new_syn_params.aperiodic_params[0] == 1.5
def test_rotate_syn_spectrum(): syn_params = SynParams([1, 1], [10, 0.5, 1], 0) freqs, spectrum = gen_power_spectrum([3, 40], *syn_params) rotated_spectrum, new_syn_params = rotate_syn_spectrum( freqs, spectrum, 0.5, 20, syn_params) assert not np.all(rotated_spectrum == spectrum) assert new_syn_params.aperiodic_params[1] == 1.5
def test_translate_spectrum(): # Create a spectrum to use for test translation freqs, spectrum = gen_power_spectrum([1, 100], [1, 1], []) # Check that translation transforms the power spectrum translated_spectrum = translate_spectrum(spectrum, delta_offset=1.) assert not np.all(translated_spectrum == spectrum) # Check that 0 translation returns the same spectrum translated_spectrum = translate_spectrum(spectrum, delta_offset=0.) assert np.all(translated_spectrum == spectrum)
def test_rotate_spectrum(): # Create a spectrum to use for test rotations freqs, spectrum = gen_power_spectrum([1, 100], [1, 1], []) # Check that rotation transforms the power spectrum rotated_spectrum = rotate_spectrum(freqs, spectrum, delta_exponent=0.5, f_rotation=25.) assert not np.all(rotated_spectrum == spectrum) # Check that 0 rotation returns the same spectrum rotated_spectrum = rotate_spectrum(freqs, spectrum, delta_exponent=0., f_rotation=25.) assert np.all(rotated_spectrum == spectrum)
Apply transformations to power spectra. """ ################################################################################################### # Imports from fooof.synth.gen import gen_power_spectrum from fooof.synth.transform import rotate_spectrum from fooof.plts.spectra import plot_spectra ################################################################################################### # Generate a synthetic power spectrum fs, ps = gen_power_spectrum([3, 40], [1, 1], [10, 0.5, 1]) ################################################################################################### # rotate_spectrum # --------------- # # The :func:`rotate_spectrum` function takes in a power spectrum, and rotates the # power spectrum a specified amount, around a specified frequency point, changing # the aperiodic exponent of the spectrum. # ################################################################################################### # Rotate the power spectrum nps = rotate_spectrum(fs, ps, 0.25, 20)
# Synthesize a group of power spectra fs, ps, syn_params = gen_group_power_spectra(n_spectra, freq_range, ap_params, gauss_params, nlv) ################################################################################################### # Print out the SynParams objects that track the parameters used to create power spectra for syn_param in syn_params: print(syn_param) ################################################################################################### # You can also use a SynParams object to regenerate a particular power spectrum cur_params = syn_params[0] fs, ps = gen_power_spectrum(freq_range, *cur_params) ################################################################################################### # Managing Parameters # ------------------- # # FOOOF provides some helper functions for managing and selecting parameters for # simulating groups of power spectra, including :func:`param_sampler` # which can be used to sample parameters from list of options, and :func:`param_iter` # which can be used to iterate across parameters. # ################################################################################################### # param_sampler # ~~~~~~~~~~~~~ #
################################################################################################### # Generate a noisy synthetic power spectrum # Set the frequency range to generate the power spectrum f_range = [1, 50] # Set aperiodic background signal parameters, as [offset, exponent] ap_params = [20, 2] # Gaussian peak parameters gauss_params = [10, 1.0, 2.5, 20, 0.8, 2, 32, 0.6, 1] # Set the level of noise to generate the power spectrum with nlv = 0.1 # Create a synthetic power spectrum freqs, spectrum = gen_power_spectrum(f_range, ap_params, gauss_params, nlv) ################################################################################################### # Fit an (unconstrained) FOOOF model, liable to overfit fm = FOOOF() fm.report(freqs, spectrum) ################################################################################################### # # Notice that in the above fit, we are very likely to think that FOOOF has # been overzealous in fitting peaks, and is therefore overfitting. # # This is also suggested by the model r-squared, which is suspiciously # high, given the amount of noise we expect. #
# # Note that all FOOOF functions that synthesize power spectra take in # gaussian parameters, not the modified peak parameters. # ################################################################################################### # Settings for creating a synthetic power spectrum freq_range = [3, 40] # The frequency range to simulate aperiodic_params = [1, 1] # Parameters defining the aperiodic component gaussian_params = [10, 0.3, 1] # Parameters for any periodic components ################################################################################################### # Generate a synthetic power spectrum fs, ps = gen_power_spectrum(freq_range, aperiodic_params, gaussian_params) ################################################################################################### # Plot the synthetic power spectrum plot_spectrum(fs, ps, log_freqs=True, log_powers=False) ################################################################################################### # Simulating With Different Parameters # ------------------------------------ # # Power spectra can be synthesized with any desired parameters for the FOOOF power spectra model. # # The aperiodic mode for the simulated power spectrum is inferred from the parameters provided. # If two parameters are provided, this is interpreted as [offset, exponent] for simulating # a power spectra with a 'fixed' aperiodic component. If three parameters are provided, as in