xbe.at

Data science

Sine and Cosine Functions in Data Analysis Time Series, Harmonic Analysis, and Signal Processing in Python:

8 min read

Introduction to Sine and Cosine Functions in Data Analysis

Sine and cosine functions, which are periodic and oscillatory in nature, play a crucial role in various data analysis techniques. These functions are particularly useful in analyzing time-series data, harmonic analysis, frequency domain analysis, and signal processing. This presentation will explore their applications and implementations using Python.

import numpy as np
import matplotlib.pyplot as plt

# Generate a time series
time = np.linspace(0, 10, 1000)
signal = np.sin(2 * np.pi * time)

# Plot the sine wave
plt.plot(time, signal)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sine Wave')
plt.show()

Time Series Analysis

Time series analysis is the study of data points collected over time. Sine and cosine functions are often used to model and analyze periodic patterns, seasonality, and trends in time series data. They can help identify underlying cycles and make predictions.

import pandas as pd
import matplotlib.pyplot as plt

# Load time series data
data = pd.read_csv('time_series_data.csv', index_col='Date', parse_dates=True)

# Plot the time series
data.plot()
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Time Series Data')
plt.show()

Harmonic Analysis

Harmonic analysis is the study of representing periodic functions as the sum of simple oscillating functions, such as sines and cosines. It is widely used in signal processing, audio analysis, and image processing.

import numpy as np
from scipy.fft import fft, ifft

# Generate a signal with multiple frequencies
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 5 * t) + np.cos(2 * np.pi * 10 * t)

# Perform Fourier transform
fft_signal = fft(signal)

# Analyze frequencies
frequencies = np.fft.fftfreq(len(signal), t[1] - t[0])

Frequency Domain Analysis

Frequency domain analysis involves transforming time-domain signals into the frequency domain using techniques like the Fourier transform. Sine and cosine functions are fundamental in representing signals in the frequency domain, enabling the identification of dominant frequencies and filtering operations.

import numpy as np
import matplotlib.pyplot as plt

# Generate a signal with multiple frequencies
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 5 * t) + np.cos(2 * np.pi * 10 * t)

# Compute the Fourier transform
fft_signal = np.fft.fft(signal)

# Analyze the frequency spectrum
frequencies = np.fft.fftfreq(len(signal), t[1] - t[0])
plt.plot(frequencies, np.abs(fft_signal))
plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.title('Frequency Spectrum')
plt.show()

Signal Processing

Signal processing involves analyzing and manipulating signals to extract meaningful information or achieve desired outcomes. Sine and cosine functions are essential for various signal processing tasks, such as filtering, modulation, and demodulation.

import numpy as np
from scipy import signal

# Generate a noisy signal
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 10 * t) + np.random.normal(0, 0.1, len(t))

# Apply a low-pass filter
cutoff_freq = 5  # Hz
nyquist_freq = 0.5 / (t[1] - t[0])
normalized_cutoff = cutoff_freq / nyquist_freq
filtered_signal = signal.filtfilt(*signal.butter(4, normalized_cutoff, btype='low'), signal)

Sine Wave Fitting

Sine wave fitting is a technique used to approximate a periodic signal or data set by fitting a sine function to it. This process can help identify the amplitude, frequency, and phase of the underlying periodic component.

import numpy as np
from scipy.optimize import curve_fit

# Generate noisy data with a sine wave
t = np.linspace(0, 10, 1000)
data = 5 * np.sin(2 * np.pi * 0.2 * t) + np.random.normal(0, 1, len(t))

# Define the sine function
def sine_func(x, amp, freq, phase):
    return amp * np.sin(2 * np.pi * freq * x + phase)

# Fit the sine function to the data
popt, _ = curve_fit(sine_func, t, data)
fitted_data = sine_func(t, *popt)

Fourier Series

The Fourier series is a way to represent periodic functions as a sum of sine and cosine functions. It is widely used in signal processing, image analysis, and other applications where periodic data needs to be analyzed or reconstructed.

import numpy as np
import matplotlib.pyplot as plt

# Define the periodic function
def square_wave(x):
    return np.where(np.fmod(x, 2 * np.pi) < np.pi, 1, -1)

# Generate the time array
t = np.linspace(0, 4 * np.pi, 1000)

# Compute the Fourier series coefficients
n = np.arange(1, 11)
a0 = 0
an = (2 / np.pi) * np.cos(n * np.pi / 2) / n
bn = 0

# Reconstruct the signal using the Fourier series
y = a0 + np.sum(an * np.cos(n * t[:, np.newaxis]), axis=1)

# Plot the original and reconstructed signals
plt.figure()
plt.plot(t, square_wave(t), label='Original')
plt.plot(t, y, label='Fourier Series')
plt.legend()
plt.show()

Discrete Fourier Transform (DFT)

The Discrete Fourier Transform (DFT) is a widely used technique for converting a finite sequence of data points into the frequency domain. It decomposes the signal into a sum of sine and cosine functions of different frequencies, enabling various signal processing operations.

import numpy as np

# Define the input signal
signal = [1, 2, 3, 4, 3, 2, 1, 0]

# Compute the DFT
N = len(signal)
dft = np.array([sum(signal * np.exp(-2j * np.pi * n * k / N) for n, x in enumerate(signal)) for k in range(N)])

# Analyze the frequency components
frequencies = np.fft.fftfreq(N, d=1)

Fast Fourier Transform (FFT)

The Fast Fourier Transform (FFT) is an efficient algorithm for computing the Discrete Fourier Transform (DFT) of a sequence. It significantly reduces the computational complexity, making it practical for analyzing large datasets and real-time applications.

import numpy as np

# Define the input signal
signal = np.random.randn(1024)

# Compute the FFT
fft_signal = np.fft.fft(signal)

# Analyze the frequency components
frequencies = np.fft.fftfreq(len(signal), d=1/1024)
amplitudes = np.abs(fft_signal)

Filtering Signals

Filtering signals is a common task in signal processing, where specific frequency components are either retained or removed from the signal. Sine and cosine functions play a crucial role in designing and implementing various filters, such as low-pass, high-pass, and band-pass filters.

import numpy as np
from scipy import signal

# Generate a noisy signal
t = np.linspace(0, 1, 1000)
signal = np.sin

Filtering Signals

Filtering signals is a common task in signal processing, where specific frequency components are either retained or removed from the signal. Sine and cosine functions play a crucial role in designing and implementing various filters, such as low-pass, high-pass, and band-pass filters.

import numpy as np
from scipy import signal

# Generate a noisy signal
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 10 * t) + np.random.normal(0, 0.5, len(t))

# Design a low-pass Butterworth filter
nyquist_freq = 0.5 / (t[1] - t[0])
cutoff_freq = 5  # Hz
normalized_cutoff = cutoff_freq / nyquist_freq
order = 4
normalized_coeffs = signal.butter(order, normalized_cutoff, btype='low', analog=False, output='ba')

# Apply the filter
filtered_signal = signal.filtfilt(normalized_coeffs[0], normalized_coeffs[1], signal)

Modulation and Demodulation

Modulation and demodulation are essential techniques in communication systems, where sine and cosine functions play a crucial role. Modulation involves encoding information onto a carrier signal, while demodulation extracts the original information from the modulated signal.

import numpy as np
import matplotlib.pyplot as plt

# Define the message signal
t = np.linspace(0, 1, 1000)
message = np.sin(2 * np.pi * 5 * t)

# Define the carrier signal
carrier_freq = 50  # Hz
carrier = np.sin(2 * np.pi * carrier_freq * t)

# Amplitude Modulation (AM)
modulated_signal = (1 + message) * carrier

# Demodulation
demodulated_signal = np.abs(signal.hilbert(modulated_signal))

# Plot the signals
plt.figure()
plt.subplot(3, 1, 1)
plt.plot(t, message)
plt.title('Message Signal')
plt.subplot(3, 1, 2)
plt.plot(t, modulated_signal)
plt.title('Modulated Signal')
plt.subplot(3, 1, 3)
plt.plot(t, demodulated_signal)
plt.title('Demodulated Signal')
plt.tight_layout()
plt.show()

Oscillatory Data Analysis

Many real-world phenomena exhibit oscillatory behavior, such as mechanical vibrations, electrical signals, and biological rhythms. Sine and cosine functions are essential for analyzing and modeling these oscillatory data, enabling the identification of frequencies, amplitudes, and phase shifts.

import numpy as np
from scipy.optimize import curve_fit

# Generate oscillatory data
t = np.linspace(0, 10, 1000)
data = 5 * np.sin(2 * np.pi * 0.5 * t + np.pi/4) + np.random.normal(0, 1, len(t))

# Define the damped sine function
def damped_sine(x, amp, freq, phase, decay):
    return amp * np.sin(2 * np.pi * freq * x + phase) * np.exp(-decay * x)

# Fit the damped sine function to the data
popt, _ = curve_fit(damped_sine, t, data, p0=[5, 0.5, np.pi/4, 0.1])
fitted_data = damped_sine(t, *popt)

Wavelet Analysis

Wavelet analysis is a powerful technique for analyzing non-stationary signals, where the frequency content varies over time. Sine and cosine functions are used to construct wavelet bases, enabling the decomposition of signals into different time-frequency components.

import numpy as np
import pywt

# Generate a signal with varying frequency
t = np.linspace(0, 10, 1000)
signal = np.sin(2 * np.pi * (1 + t/10) * t)

# Perform continuous wavelet transform
coeffs, freqs = pywt.cwt(signal, np.arange(1, 11), 'morl')

# Plot the wavelet scalogram
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(np.abs(coeffs), extent=[0, 10, 1, 11], cmap='jet', aspect='auto')
plt.colorbar()
plt.xlabel('Time')
plt.ylabel('Scale')
plt.title('Wavelet Scalogram')
plt.show()

Additional Resources

For those interested in exploring further, here are some additional resources on the applications of sine and cosine functions in data analysis:

These resources from ArXiv provide in-depth insights and advanced techniques for working with sine and cosine functions in data analysis contexts.

More in Data science

  1. Strategies for Handling Missing Data Types Using Python11 min

    Missing data is a common challenge in data analysis. Understanding the types of missing data and appropriate strategies for handling them is crucial for...

  2. Roadmap of Data Science with Python10 min

    Data science is an interdisciplinary field that uses scientific methods, processes, algorithms, and systems to extract knowledge and insights from structured...

  3. The Beast of Real-World Data Wrangling11 min

    Data wrangling in real-world scenarios is indeed more challenging than working with clean, pre-processed datasets like iris or mtcars. Real-world data often...

  4. Regex Operations for Data Science in Python7 min

    Regular expressions (regex) are powerful tools for pattern matching and text manipulation in data science. They allow you to search, extract, and transform...

  5. Time Shifting in Pandas Manipulating Time Series Data7 min

    Time shifting is a powerful feature in pandas that allows you to manipulate and analyze time series data efficiently. It enables you to shift index labels...

  6. Reasons to Migrate from Pandas to FireDucks8 min

    FireDucks represents a revolutionary advancement in data manipulation frameworks, offering seamless compatibility with existing Pandas code while delivering...