<jupyter_start><jupyter_text>Import Section<jupyter_code>import numpy as np
from syncsweptsine import SyncSweep, LinearModel, FrfFilterKernel, HammersteinModel<jupyter_output><empty_output><jupyter_text>Import Virtual Sound Field<jupyter_code>SOUND_FIELD = lambda x: 2. * x.copy()

# from pysndfx import AudioEffectsChain
# SOUND_FIELD = AudioEffectsChain().reverb()<jupyter_output><empty_output><jupyter_text>import Warning Controller<jupyter_code>from warnings import simplefilter
simplefilter('ignore')<jupyter_output><empty_output><jupyter_text>import Plotters<jupyter_code>from matplotlib import pylab as plt
import seaborn as sns

sns.set()
sns.set_context('paper')
sns.set_palette('Set2', 4)
# plt.figure(dpi=200)<jupyter_output><empty_output><jupyter_text>Define Global Variables  Sample rate [Hz]<jupyter_code>RATE = 48000<jupyter_output><empty_output><jupyter_text>Total Time Length of Source [sec]<jupyter_code>LEN_T = 180<jupyter_output><empty_output><jupyter_text>Freq of Source [Hz]<jupyter_code>FREQ = 440<jupyter_output><empty_output><jupyter_text>Time-Axis<jupyter_code>t = np.linspace(0, LEN_T, LEN_T*RATE, dtype=np.float64)<jupyter_output><empty_output><jupyter_text>About FIR-Filter  Filter Length<jupyter_code>LEN_FIR = 2048<jupyter_output><empty_output><jupyter_text>Make Source-Data<jupyter_code># A = np.finfo(np.float64).max / 2.
A = 2.0
wave = np.sin(2. * np.pi * FREQ * t) + .5 * np.sin(2. * np.pi * 3. * FREQ * t)
rawdata = A * wave
plt.plot(t[:FREQ], rawdata[:FREQ])
plt.xlabel('Time[s]')
plt.ylabel('Amplitude')
plt.show()<jupyter_output><empty_output><jupyter_text>Main<jupyter_code>ssweep = SyncSweep(startfreq=10, stopfreq=RATE//2, durationappr=10, samplerate=RATE)
indata = np.array(ssweep, dtype=np.float64)
print(ssweep, indata.shape)
plt.plot(ssweep)
plt.xlabel('Time[s]')
plt.ylabel('Amplitude')
plt.show()
plt.specgram(ssweep, NFFT=2048, Fs=RATE)
plt.title('Spectrogram of Sweep')
plt.colorbar(label='Power')
plt.xlabel('Time[s]')
plt.show()
outdata = SOUND_FIELD(indata)
outdata.shape
# hhir = HigherHarmonicImpulseResponse.from_sweeps(syncsweep=ssweep, measuredsweep=outdata)
# hhir
model = HammersteinModel.from_sweeps(syncsweep=ssweep, measuredsweep=outdata, orders=[1,])
kernel = model.kernels[0]
plt.plot(kernel.frf)
plt.show()
FRFinv = 1. / kernel.frf
# FRFinv = np.conj(kernel.frf)
plt.plot(FRFinv)
plt.show()
from scipy.fft import irfft
frf_raw = irfft(kernel.frf, n=RATE)
frf_inv = irfft(FRFinv, n=RATE)
plt.plot(frf_inv, label='FIR Inverse Filter')
plt.show()
plt.plot(frf_raw, label='Original FIR Filter')
plt.show()
from scipy.signal import convolve
# indata_modified = convolve(indata, frf_inv)
indata_modified = convolve(rawdata, frf_inv, method='fft')
outdata_modified = SOUND_FIELD(indata_modified)
# indata.shape, indata_modified.shape, outdata.shape, outdata_modified.shape<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code>from scipy.fft import fft
plt.plot(20.*np.log10(np.abs(fft(frf_inv))), label='Impulse Response')
plt.xscale('log')
plt.xlabel('Frequency[Hz]')
plt.ylabel('Gain[dB]')
# plt.ylim([-10, 30])
plt.legend()
plt.show()
plt.plot(t[-FREQ:], outdata_modified[-FREQ:], label='Output FROM sound-field')
plt.plot(t[-FREQ:], rawdata[-FREQ:], label='Input')
plt.xlabel('Time[s]')
plt.ylabel('Amplitude')
plt.legend()
plt.tight_layout()
# plt.show()
plt.savefig('/mnt/c/Users/kmise/result_waves_sweep.png', format='png', dpi=400)
plt.plot(20.*np.log10(np.abs(fft(outdata_modified,))), label='Output FROM sound-field')
# plt.plot(20.*np.log10(np.abs(fft(indata_modified,))), label='Output FOR sound-field')
plt.plot(20.*np.log10(np.abs(fft(rawdata,))), label='Input')
plt.xscale('log')
# plt.ylim([0, 100])
plt.xlabel('Frequency[Hz]')
plt.ylabel('Gain[dB]')
plt.legend()
plt.tight_layout()
# plt.show()
plt.savefig('/mnt/c/Users/kmise/result_sweep.png', format='png', dpi=400)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16306027820710975, 'fraction_numerical': 0.01983513652756311, 'mean_word_length': 8.517156862745098, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '587645', 'license': 'MIT', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1593, 'n_words': 226, 'path': 'notebooks/syncsweptsine.ipynb', 'repo_name': 'borley1211/adaptune', 'size': '3882'}