Add a random normal distribution noise into the signal in Problem 5 using NumPy and plot the FFT amplitude spectrum. What do you see? The signal with noise will be shown in the following test case. Test case:
In [2]: np.random.seed(10)
x_noise
np.random.normal(0, 2. size len(x))
plt.figure(figsize (8.6))
plt.plot(t, x_noise, “r”)
plt.ylabel(“Amplitude”)
plt.xlabel(“Time (s)”)
plt.show()

Generate two signals: signal 1 is a sine wave with 5 Hz, amplitude 3, and phase shift 3, and signal 2 is a sine wave with 2 Hz, amplitude 2, and phase shift −2. Plot the signal for 2 s. Test cases:
In [1]: sampling rate
sr 100
sampling interval
ts-1.0/sr
t np.arange(0.2.ts)
freq-5.
x-3-np.sin(2-np.pl-freq-t + 3)
freq 2
x+2+np.sin(2-np.pi freq-t2)
plt.figure(figsize (8.6))
plt.plot(t, x. “r”)
plt.ylabel(“Amplitude”)
plt.xlabel(“Time (s)”)
plt.show()

The Correct Answer and Explanation is :
To analyze the effect of adding random noise to a signal and observe its impact on the frequency spectrum, we can follow these steps using Python’s NumPy and Matplotlib libraries.
1. Generate the Original Signal:
We’ll create two sine waves with specified frequencies, amplitudes, and phase shifts, then combine them to form the original signal.
import numpy as np
import matplotlib.pyplot as plt
# Sampling rate and time vector
sr = 100 # Sampling rate in Hz
t = np.arange(0, 2, 1/sr) # Time vector from 0 to 2 seconds
# Signal 1: 5 Hz sine wave with amplitude 3 and phase shift 3 radians
freq1 = 5
x1 = 3 * np.sin(2 * np.pi * freq1 * t + 3)
# Signal 2: 2 Hz sine wave with amplitude 2 and phase shift -2 radians
freq2 = 2
x2 = 2 * np.sin(2 * np.pi * freq2 * t - 2)
# Combined signal
x = x1 + x2
# Plot the original signal
plt.figure(figsize=(8, 6))
plt.plot(t, x, 'r')
plt.title('Original Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()
2. Add Random Normal Distribution Noise:
Next, we’ll add random noise with a mean of 0 and a standard deviation of 2 to the original signal.
# Set random seed for reproducibility
np.random.seed(10)
# Generate random noise
noise = np.random.normal(0, 2, len(x))
# Signal with added noise
x_noise = x + noise
# Plot the noisy signal
plt.figure(figsize=(8, 6))
plt.plot(t, x_noise, 'r')
plt.title('Signal with Added Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()
3. Compute and Plot the FFT Amplitude Spectrum:
We’ll compute the Fast Fourier Transform (FFT) of the noisy signal to analyze its frequency components.
# Compute the FFT of the noisy signal
X = np.fft.fft(x_noise)
N = len(X)
frequencies = np.fft.fftfreq(N, 1/sr)
# Compute the amplitude spectrum (magnitude of the FFT)
amplitude_spectrum = np.abs(X)[:N//2]
frequencies = frequencies[:N//2]
# Plot the amplitude spectrum
plt.figure(figsize=(8, 6))
plt.plot(frequencies, amplitude_spectrum)
plt.title('FFT Amplitude Spectrum of Noisy Signal')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()
Explanation:
- Original Signal: The combined signal consists of two sine waves with frequencies of 5 Hz and 2 Hz, amplitudes of 3 and 2, and phase shifts of 3 and -2 radians, respectively.
- Added Noise: Random noise with a mean of 0 and a standard deviation of 2 is added to the original signal. This noise introduces random fluctuations across all frequencies.
- FFT Analysis: The FFT converts the time-domain signal into the frequency domain, allowing us to observe the frequency components present in the signal. The amplitude spectrum represents the magnitude of each frequency component.
- Observations: In the amplitude spectrum of the noisy signal, the peaks corresponding to the original signal’s frequencies (5 Hz and 2 Hz) are still present but may be less pronounced due to the added noise. Additionally, there is a broad spectrum of frequencies introduced by the random noise, resulting in a more complex frequency profile.
This analysis demonstrates how adding random noise affects the frequency characteristics of a signal, making it more challenging to identify specific frequency components.
For a visual demonstration of denoising data using FFT in Python, you might find the following video helpful:
videoDenoising Data with FFT [Python]turn0search5