<jupyter_start><jupyter_text>Downsampling<jupyter_code>f0 = 5000.
fs = 22500.
ns = 30.
n = np.linspace(0, ns - 1, ns)
x = (np.cos(2 * np.pi * f0 / fs * n) + np.cos(2 * np.pi * 0.5 * f0 / fs * n))
# x=np.random.randn(ns)
# x=np.arange(1,7)
x
# FIR low pass
M = 3
N = 4
fpass = 2000
fstop = 3000
h = sp.remez(N, np.array([0, fpass, fstop, fs / (2 * M)]), [1, 0], Hz=fs)

# Pass through Low pass filter with cutoff fs/2*M --> Drop every M sample. 

def drop(x, M):
    '''
    Drop every M sample in x.
    '''
    if len(x) % 2 != 0:
        l = len(x) + 1
    else:
        l = len(x)
    sig = [x[i * M] for i in range(int(l / M))]
    return np.array(sig)

x_ = drop(np.convolve(x, h), M) # convolve and pass through low pass

n_ = np.linspace(0, len(x_) - 1, len(x_))


plt.subplot(2, 1, 1)
plt.stem(n, x)
plt.xlim(0, max(n))
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.title("Input")
plt.subplot(2, 1, 2)
plt.stem(n_, x_)
plt.xlim(0, max(n_))
plt.title("Output")
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.tight_layout()
plt.show()
# Plot response
X = np.fft.fft(x)
X_ = np.fft.fft(x_)
normf = n / ns

normf_ = n_ / float(len(X_))

plt.subplot(2, 1, 1)
plt.plot(normf, abs(X), label="Input")
plt.plot(normf_, abs(X_), label="Output")
plt.xlim(0, max(normf_))
plt.legend(loc=1)
plt.xlabel("Normalised freq")
plt.ylabel("Response")
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Upsampling<jupyter_code>L = 2
N = 4
fpass = 2000
fstop = 3000
h = sp.remez(N, np.array([0, fpass, fstop, fs]), [1, 0], Hz=L * fs)
# Pad sequence with L-1 0 samples--> Pass through Low pass filter with cutoff fs
def inc_pad(x, L):
    '''
    Insert L-1 0 samples between every sample in x.
    '''
    size = (L - 1) * (len(x) - 1) + len(x)
    z = np.zeros(size,)
    for i in range(len(x)):
        z[i * L] = x[i]
    return z
    
u = inc_pad(x, L) 
x_ = np.convolve(inc_pad(x, L), h)
n_ = np.linspace(0, len(x_) - 1, len(x_))


plt.subplot(2, 1, 1)
plt.stem(n, x)
plt.xlim(0, max(n))
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.title("Input")
plt.subplot(2, 1, 2)
plt.stem(n_, x_)
plt.xlim(0, max(n_))
plt.title("Output")
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.tight_layout()
plt.show()
# Plot response
X = np.fft.fft(x)
X_ = np.fft.fft(x_)

normf = n / ns

normf_ = n_ / float(len(X_))


plt.plot(normf, abs(X), label="Input")
plt.plot(normf_, abs(X_), label="Output")
plt.xlim(0, max(normf_))
plt.legend(loc=1)
plt.xlabel("Normalised freq")
plt.ylabel("Response")
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Sampling by non-integer value  Sampling by m/n - ```if m > n :     Upsample by m then Downsample by n.else :     Downsample by n then Upsample by m. ```  Sampling by 2.5 i.e Upsampling by 5 and Downsampling by 2<jupyter_code>L = 5  # upsample
M = 2  # downsample
N = 4
fpass = 2000
fstop = 3000
h1 = sp.remez(N, np.array([0, fpass, fstop, fs]), [1, 0], Hz=L * fs)
h2 = sp.remez(N, np.array(
    [0, fpass, fstop, (L * fs) / (2. * M)]), [1, 0], Hz=L * fs)
# upsampling by 5
y1 = np.convolve(inc_pad(x, L), h1)

# downsampling by 2
y2 = drop(np.convolve(y1, h2), M)

n1 = np.linspace(0, len(y1) - 1, len(y1))
n2 = np.linspace(0, len(y2) - 1, len(y2))


plt.figure(figsize=(6, 6))
plt.subplot(3, 1, 1)
plt.stem(n, x)
plt.xlim(0, max(n))
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.title("Input")
plt.subplot(3, 1, 2)
plt.stem(n1, y1)
plt.xlim(0, max(n1))
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.title("Interim (upsample by 5)")
plt.subplot(3, 1, 3)
plt.stem(n2, y2)
plt.xlim(0, max(n2))
plt.title("Output (downsample by 2)")
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.tight_layout()
plt.show()
# Plot response
X = np.fft.fft(x)
Y1 = np.fft.fft(y1)
Y2 = np.fft.fft(y2)

normf = n / ns
normf1 = n1 / float(len(y1))
normf2 = n2 / float(len(y2))


plt.plot(normf, abs(X), label="Input")
plt.plot(normf1, abs(Y1), label="Interim")
plt.plot(normf2, abs(Y2), label="Output")
plt.xlim(0, max(normf))
plt.legend(loc=1)
plt.xlabel("Normalised freq")
plt.ylabel("Response")
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2036764705882353, 'fraction_numerical': 0.04044117647058824, 'mean_word_length': 3.8815789473684212, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '479219', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1899, 'n_words': 442, 'path': 'Multirate_sp.ipynb', 'repo_name': 'ishank26/Digital-Signal-Processing', 'size': '4080'}