<jupyter_start><jupyter_text>Find a FIR filter which best inverts the HRF.<jupyter_code>def find_fir(hrf, ntap):
    y = np.zeros(hrf.size)
    y[0] = 1.0
    
    hrfs = []
    for i in range(len(hrf)):
        hrfs.append(np.roll(hrf, i))
        
    the_mat = np.stack(hrfs, axis=0)
    plt.imshow(hrfs)
    plt.show()
    
    best_pos = np.argmax(hrf)
    H = np.zeros((y.size, y.size))
    idx = ntap //2
    H[:, (best_pos-idx):(best_pos+idx+1)] = 1
    #H = 1
    the_mat = the_mat * H
    
    print(best_pos-idx)
    
    lambda_ = .001
    w, _, _, _ = np.linalg.lstsq(the_mat.T.dot(the_mat) + lambda_ * np.eye(hrf.size), the_mat.T.dot(y.reshape((-1, 1))), rcond=None)
    return w

w = find_fir(hrf, 7)
w = np.fft.fftshift(w)

import scipy
import scipy.signal
plt.plot(scipy.signal.convolve2d(hrf.reshape((-1, 1)), 
                                 w[::-1].reshape((-1, 1))))
plt.show()

plt.plot(w)
(w[abs(w) > .1] / w.max())[::-1]
w.sum()

#plt.xlim((200, 300))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18986083499005965, 'fraction_numerical': 0.027833001988071572, 'mean_word_length': 2.4844290657439445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '380952', 'license': 'MIT', 'n_tokens_mistral': 486, 'n_tokens_neox': 463, 'n_words': 76, 'path': 'scripts/retired/Estimate HRF.ipynb', 'repo_name': 'patrickmineault/brain-scorer', 'size': '1006'}