How to Compute the DFT of a Sine Wave in Python?

Introduction to DFT and Sine Waves In the realm of signal processing, the Discrete Fourier Transform (DFT) is a powerful tool used to analyze the frequencies contained in a sampled signal. As an example, let’s explore how to compute the DFT of a simple sine wave with a frequency of 3 Hz. This task will illustrate the effects of sampling duration on frequency analysis. We'll use Python's popularity for numerical analysis to implement this. The sine function we will use for our test is defined as: sin_function = sin(2 * pi * 3 * t) Here, the sine wave oscillates at 3 Hz. We'll set up a sample rate of 15 Hz, which means we will sample the sine wave 15 times per second. Our time interval between samples will, therefore, be: interval = 1 / sample_rate # ~0.067 seconds We will examine two durations for sampling the sine wave: 1 second and 2 seconds, leading us to a sample size of 15 for the first test and 30 for the second. Understanding the Sampling Process When we run our code with a sample duration of 1 second, we should accurately capture the frequency of 3 Hz. However, increasing the sample duration to 2 seconds introduces an unexpected peak at frequency 6 Hz (which is double the original frequency). This phenomenon can be attributed to aliasing, where higher frequency signals can distort the expected output as the sample size increases. How Sampling Duration Affects DFT The DFT utilizes the sampled data to analyze frequency components. The peak frequencies seen in the DFT results depend heavily on the sampling rate and the total duration of the samples taken. For instance, with a sample duration of 3 seconds, you could expect to see even higher peaks such as 9 Hz. This leads to the conclusion that simply increasing sample duration doesn't always yield finer results; it can, in fact, complicate frequency analysis due to phenomena like aliasing. Implementing the DFT in Python Below is the complete Python code needed to compute the DFT of a sine wave, assessing the effects of sampling duration: from sage.all import * import matplotlib.pyplot as plt import numpy as np # Define parameters sample_rate = 15 interval = 1 / sample_rate sample_duration_1 = 1 sample_duration_2 = 2 sample_size_N_1 = sample_rate * sample_duration_1 sample_size_N_2 = sample_rate * sample_duration_2 # Define the sine function t = var('t') func = sin(2 * pi * 3 * t) # Function to compute samples def compute_samples(sample_duration): time_segment_arr = [] signal_sample_arr = [] for time_segment in np.arange(0, sample_duration, interval): discrete_signal_value = func(t=time_segment) time_segment_arr.append(time_segment) signal_sample_arr.append(N(discrete_signal_value)) return signal_sample_arr, len(signal_sample_arr) # Compute DFT function def construct_dft_func(signal_sample_arr, sample_size_N): s = '' k = var('k') for n in range(0, sample_size_N): s = s + '+' + str((signal_sample_arr[n]) * e^(-(i * 2 * pi * k * n) / sample_size_N)) return s[1:] # Function to calculate frequencies def calculate_frequencies(signal_sample_arr, sample_size_N): dft_func = construct_dft_func(signal_sample_arr, sample_size_N) freq_arr = [] amplitude_arr = [] for l in np.arange(0, sample_size_N): freq_value = calculate_frequency_value(dft_func, l) freq_arr.append(l) amplitude_arr.append(N(abs(freq_value))) return freq_arr, amplitude_arr # Perform calculations for both sampling durations signal_samples_duration_1, size_1 = compute_samples(sample_duration_1) freq_1, amplitude_1 = calculate_frequencies(signal_samples_duration_1, size_1) signal_samples_duration_2, size_2 = compute_samples(sample_duration_2) freq_2, amplitude_2 = calculate_frequencies(signal_samples_duration_2, size_2) # Plotting results plt.subplot(2, 1, 1) plt.plot(freq_1, amplitude_1) plt.title('DFT for Duration 1 Second') plt.subplot(2, 1, 2) plt.plot(freq_2, amplitude_2) plt.title('DFT for Duration 2 Seconds') plt.show() Analyzing the Output Upon executing this code, you'll notice two graphs corresponding to 1-second and 2-second sample durations. The first graph rightfully peaks at 3 Hz while the second introduces an artifact peak at higher frequencies. This outcome emphasizes the significance of understanding sampling principles and their implications on Fourier analysis. Conclusion In summary, while it might seem intuitive that longer sample durations yield more accurate measurements, it’s crucial to recognize the rules of sampling and how they influence DFT results. The presence of peak frequencies that are not part of the original signal indicates that lengthening the sample could lead to inappropriate interpretations of the frequency domain. Frequently Asked Questions (FAQ) What is DFT? The Discrete Fourier Transform (DFT) converts a sequence of sampl

May 11, 2025 - 21:32
 0
How to Compute the DFT of a Sine Wave in Python?

Introduction to DFT and Sine Waves

In the realm of signal processing, the Discrete Fourier Transform (DFT) is a powerful tool used to analyze the frequencies contained in a sampled signal. As an example, let’s explore how to compute the DFT of a simple sine wave with a frequency of 3 Hz. This task will illustrate the effects of sampling duration on frequency analysis. We'll use Python's popularity for numerical analysis to implement this.

The sine function we will use for our test is defined as:

sin_function = sin(2 * pi * 3 * t)

Here, the sine wave oscillates at 3 Hz. We'll set up a sample rate of 15 Hz, which means we will sample the sine wave 15 times per second. Our time interval between samples will, therefore, be:

interval = 1 / sample_rate  # ~0.067 seconds

We will examine two durations for sampling the sine wave: 1 second and 2 seconds, leading us to a sample size of 15 for the first test and 30 for the second.

Understanding the Sampling Process

When we run our code with a sample duration of 1 second, we should accurately capture the frequency of 3 Hz. However, increasing the sample duration to 2 seconds introduces an unexpected peak at frequency 6 Hz (which is double the original frequency). This phenomenon can be attributed to aliasing, where higher frequency signals can distort the expected output as the sample size increases.

How Sampling Duration Affects DFT

The DFT utilizes the sampled data to analyze frequency components. The peak frequencies seen in the DFT results depend heavily on the sampling rate and the total duration of the samples taken. For instance, with a sample duration of 3 seconds, you could expect to see even higher peaks such as 9 Hz. This leads to the conclusion that simply increasing sample duration doesn't always yield finer results; it can, in fact, complicate frequency analysis due to phenomena like aliasing.

Implementing the DFT in Python

Below is the complete Python code needed to compute the DFT of a sine wave, assessing the effects of sampling duration:

from sage.all import *  
import matplotlib.pyplot as plt  
import numpy as np  

# Define parameters  
sample_rate = 15  
interval = 1 / sample_rate  
sample_duration_1 = 1  
sample_duration_2 = 2  
sample_size_N_1 = sample_rate * sample_duration_1  
sample_size_N_2 = sample_rate * sample_duration_2  

# Define the sine function  
t = var('t')  
func = sin(2 * pi * 3 * t)  

# Function to compute samples  
def compute_samples(sample_duration):  
    time_segment_arr = []  
    signal_sample_arr = []  
    for time_segment in np.arange(0, sample_duration, interval):  
        discrete_signal_value = func(t=time_segment)  
        time_segment_arr.append(time_segment)  
        signal_sample_arr.append(N(discrete_signal_value))  
    return signal_sample_arr, len(signal_sample_arr)  

# Compute DFT function  
def construct_dft_func(signal_sample_arr, sample_size_N):  
    s = ''  
    k = var('k')  
    for n in range(0, sample_size_N):  
        s = s + '+' + str((signal_sample_arr[n]) * e^(-(i * 2 * pi * k * n) / sample_size_N))  
    return s[1:]  

# Function to calculate frequencies  
def calculate_frequencies(signal_sample_arr, sample_size_N):  
    dft_func = construct_dft_func(signal_sample_arr, sample_size_N)  
    freq_arr = []  
    amplitude_arr = []  
    for l in np.arange(0, sample_size_N):  
        freq_value = calculate_frequency_value(dft_func, l)  
        freq_arr.append(l)  
        amplitude_arr.append(N(abs(freq_value)))  
    return freq_arr, amplitude_arr  

# Perform calculations for both sampling durations  
signal_samples_duration_1, size_1 = compute_samples(sample_duration_1)  
freq_1, amplitude_1 = calculate_frequencies(signal_samples_duration_1, size_1)  

signal_samples_duration_2, size_2 = compute_samples(sample_duration_2)  
freq_2, amplitude_2 = calculate_frequencies(signal_samples_duration_2, size_2)  

# Plotting results  
plt.subplot(2, 1, 1)  
plt.plot(freq_1, amplitude_1)  
plt.title('DFT for Duration 1 Second')  
plt.subplot(2, 1, 2)  
plt.plot(freq_2, amplitude_2)  
plt.title('DFT for Duration 2 Seconds')  
plt.show()  

Analyzing the Output

Upon executing this code, you'll notice two graphs corresponding to 1-second and 2-second sample durations. The first graph rightfully peaks at 3 Hz while the second introduces an artifact peak at higher frequencies. This outcome emphasizes the significance of understanding sampling principles and their implications on Fourier analysis.

Conclusion

In summary, while it might seem intuitive that longer sample durations yield more accurate measurements, it’s crucial to recognize the rules of sampling and how they influence DFT results. The presence of peak frequencies that are not part of the original signal indicates that lengthening the sample could lead to inappropriate interpretations of the frequency domain.

Frequently Asked Questions (FAQ)

What is DFT?

The Discrete Fourier Transform (DFT) converts a sequence of samples into the frequency domain, allowing one to analyze frequency components in digital signals.

Why does aliasing occur in DFT?

Aliasing happens when the sample rate is insufficient to capture the frequency of interest. It leads to the misrepresentation of the actual frequency in the DFT output.

How to avoid aliasing?

To avoid aliasing, ensure that your sampling rate is at least twice the highest frequency present in the signal, a principle known as the Nyquist theorem.