<jupyter_start><jupyter_text>Creating Data<jupyter_code>fs = 2000 #Hz
t = np.linspace(0, 2, 2*fs) #seconds, time
l_freq = np.sin(10*2*np.pi*t) #10 Hz
h_freq = np.sin(500*2*np.pi*t) #500 Hz
nt = np.sin(60*2*np.pi*t) #60 Hz
signal = l_freq + h_freq + nt

#Plotting 
plt.title("Time Domain")
plt.plot(t, signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("Frequency Domain")
f = np.linspace(0, t.shape[0]/2., t.shape[0])
plt.plot(f,np.abs(np.fft.fft(signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output><empty_output><jupyter_text>Filters (in frequency domain)<jupyter_code>def HP(signal, fc, fs):
    s_kernel = int(round(signal.shape[0]*(fc/fs)))
    kernel = np.hstack((np.zeros(s_kernel),np.ones(signal.shape[0]- 2*s_kernel), np.zeros(s_kernel)))
    Y = np.multiply(kernel, np.fft.fft(signal))
    return np.fft.ifft(Y).real


def LP(signal, fc, fs):
    s_kernel = int(round(signal.shape[0]*(fc/fs)))
    kernel = np.hstack((np.ones(s_kernel), np.zeros(signal.shape[0]- 2*s_kernel), np.ones(s_kernel)))
    Y = np.multiply(kernel, np.fft.fft(signal))
    return np.fft.ifft(Y).real


def notch(signal, fc, q, fs):
    s_kernel = int(round(signal.shape[0]*(fc/fs)))
    kernel = np.hstack((np.ones(s_kernel - int(q/2)), np.zeros(q), np.ones(signal.shape[0]-q - 2*s_kernel), np.zeros(q),np.ones(s_kernel - int(q/2))))
    Y = np.multiply(kernel, np.fft.fft(signal))
    return np.fft.ifft(Y).real<jupyter_output><empty_output><jupyter_text>Testing and timing  Low pass<jupyter_code>start = time.time()
lp_signal  = LP(signal, 450., fs)
end = time.time()

print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"

#Plotting 
plt.title("New time Domain")
plt.plot(t, lp_signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("New frequency Domain")
plt.plot(f,np.abs(np.fft.fft(lp_signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output>Processing time:  0.00144100189209 seconds
Processing time per sample:  3.60250473022e-07 seconds<jupyter_text>High pass<jupyter_code>start = time.time()
hp_signal  = HP(signal, 15., fs)
end = time.time()

print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"

#Plotting 
plt.title("New time Domain")
plt.plot(t, hp_signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("New frequency Domain")
plt.plot(f,np.abs(np.fft.fft(hp_signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output>Processing time:  0.0010449886322 seconds
Processing time per sample:  2.61247158051e-07 seconds<jupyter_text>Notch filter<jupyter_code>start = time.time()
no_signal  = notch(signal, 60., 10, fs)
end = time.time()

print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"

#Plotting 
plt.title("New time Domain")
plt.plot(t, no_signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("New frequency Domain")
plt.plot(f,np.abs(np.fft.fft(no_signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output>Processing time:  0.000933885574341 seconds
Processing time per sample:  2.33471393585e-07 seconds<jupyter_text>All filters<jupyter_code>start = time.time()
all_signal  = HP(LP(notch(signal, 60., 10, fs), 450, fs), 15, fs)
end = time.time()

print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"

#Plotting 
plt.title("New time Domain")
plt.plot(t, all_signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("New frequency Domain")
plt.plot(f,np.abs(np.fft.fft(all_signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output>Processing time:  0.00194478034973 seconds
Processing time per sample:  4.86195087433e-07 seconds<jupyter_text>Filters by convolution<jupyter_code>#These functions return the sinc(t), convolution must be used
def HP_conv(signal_size, fc, fs):
    s_kernel = int(round(signal_size*(fc/fs)))
    kernel = np.hstack((np.zeros(s_kernel),np.ones(signal_size- 2*s_kernel), np.zeros(s_kernel)))
    return np.fft.fftshift(np.fft.ifft(kernel).real)


def LP_conv(signal_size, fc, fs):
    s_kernel = int(round(signal.shape[0]*(fc/fs)))
    kernel = np.hstack((np.ones(s_kernel), np.zeros(signal_size- 2*s_kernel), np.ones(s_kernel)))
    return np.fft.fftshift(np.fft.ifft(kernel).real)


def notch_conv(signal_size, fc, q, fs):
    s_kernel = int(round(signal.shape[0]*(fc/fs)))
    kernel = np.hstack((np.ones(s_kernel - int(q/2)), np.zeros(q), np.ones(signal_size-q - 2*s_kernel), np.zeros(q),np.ones(s_kernel - int(q/2))))
    return np.fft.fftshift(np.fft.ifft(kernel).real)<jupyter_output><empty_output><jupyter_text>Testing and timing  Visualizing  the sinc(t) function<jupyter_code>start = time.time()
sinc_hp = HP_conv(signal.shape[0], 15., fs)
end = time.time()
plt.title("High pass")
plt.plot(sinc_hp)
plt.show()
print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"


start = time.time()
sinc_lp = LP_conv(signal.shape[0], 450., fs)
end = time.time()
plt.title("Low pass")
plt.plot(sinc_lp)
plt.show()
print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"


start = time.time()
sinc_no = notch_conv(signal.shape[0], 60.,20, fs)
end = time.time()
plt.title("Notch")
plt.plot(sinc_no)
plt.show()
print "Processing time: ", end-start, "seconds"
print "Processing time per sample: ", (end-start)/signal.shape[0], "seconds"<jupyter_output><empty_output><jupyter_text>High pass by conv<jupyter_code>start = time.time()
hp_signal  = np.convolve(HP_conv(signal.shape[0], 15., fs), signal, 'same')
end = time.time()
print "Processing time: ", end-start, "seconds"

#Partial time
start = time.time()
sinc = HP_conv(signal.shape[0], 15., fs)
end = time.time()
p1 = end - start
start = time.time()
hp_signal  = np.convolve(sinc, signal, 'same')
end = time.time()
p2 = end - start
print "Partial 1:", p1, "Partial 2:", p2
print "Total:", p1+p2

#Plotting 
plt.title("New time Domain")
plt.plot(t, hp_signal)
plt.xlabel("Time(s)")
plt.show()

plt.title("New frequency Domain")
plt.plot(f,np.abs(np.fft.fft(hp_signal)))
plt.xlabel("Frequency (Hz)")
plt.xlim(0, fs/2) #showing  only the useful part
plt.show()<jupyter_output>Processing time:  0.0041708946228 seconds
Partial 1: 0.000617980957031 Partial 2: 0.00300812721252
Total: 0.00362610816956
{'fraction_non_alphanumeric': 0.17730815347721823, 'fraction_numerical': 0.04301558752997602, 'mean_word_length': 5.5229716520039105, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '634013', 'license': 'MIT', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2659, 'n_words': 528, 'path': 'Filters.ipynb', 'repo_name': 'ithallojunior/dig_filters', 'size': '6672'}