Complete the code to generate a sampled signal from a continuous sine wave.
import numpy as np import matplotlib.pyplot as plt fs = 1000 # Sampling frequency in Hz f = 5 # Signal frequency in Hz t = np.linspace(0, 1, fs, endpoint=False) continuous_signal = np.sin(2 * np.pi * f * t) sample_points = t[::[1]] sampled_signal = continuous_signal[::[1]] plt.stem(sample_points, sampled_signal, use_line_collection=True) plt.title('Sampled Signal') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.show()
Sampling every 10th point reduces the sampling rate to 100 Hz, which is sufficient to capture a 5 Hz signal.
Complete the code to reconstruct the signal using sinc interpolation.
import numpy as np import matplotlib.pyplot as plt fs = 100 # Sampling frequency f = 5 # Signal frequency T = 1 / fs n = np.arange(0, 1, T) sampled_signal = np.sin(2 * np.pi * f * n) # Reconstruction time points t = np.linspace(0, 1, 1000) reconstructed_signal = np.zeros_like(t) for i, ti in enumerate(t): reconstructed_signal[i] = sum(sampled_signal * np.sinc((ti - n) / [1])) plt.plot(t, reconstructed_signal) plt.title('Reconstructed Signal') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.show()
The sinc interpolation uses the sampling period T as the scaling factor in the sinc function.
Fix the error in the code that calculates the Nyquist frequency.
fs = 800 # Sampling frequency in Hz nyquist_freq = [1] print(f"Nyquist frequency is {nyquist_freq} Hz")
The Nyquist frequency is half the sampling frequency.
Fill both blanks to create a dictionary of sampled points and their amplitudes for frequencies below Nyquist.
fs = 500 nyquist = fs / 2 frequencies = [100, 200, 300, 400] sampled_points = {f: f * [1] for f in frequencies if f < [2] print(sampled_points)
The amplitude is scaled by 0.01, and frequencies below the Nyquist frequency are included.
Fill the blanks to create a dictionary comprehension that maps time points to sampled sine values only if amplitude is above threshold.
import numpy as np fs = 1000 f = 10 t = np.linspace(0, 1, fs, endpoint=False) sampled_signal = {t_i: np.sin(2 * np.pi * f * t_i) for t_i in t if np.sin(2 * np.pi * f * t_i) [1] [2] print(list(sampled_signal.items())[:5])
The dictionary includes time points where the sine amplitude is greater than 0.5.
