Skip to content

rapzyftminji/cwt-signal-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Continuous Wavelet Transform (CWT) From Scratch

An interactive Streamlit web application for performing Continuous Wavelet Transform (CWT) analysis and Fast Fourier Transform (FFT) on signal data, with a focus on Phonocardiogram (PCG) signal analysis.

Features

Signal Input

  • Upload Custom Data: Load 2-column .txt files (time and signal values)
  • Generate Synthetic Signals: Create sine waves with customizable parameters:
    • Two independent sine wave components
    • Adjustable frequency, amplitude, and noise levels
    • Configurable sampling frequency and duration

Signal Analysis

  1. Time-Domain Analysis

    • Normalized signal visualization
    • Shannon Energy envelope computation
    • Interactive plots with zoom capabilities
  2. Frequency-Domain Analysis (FFT)

    • Fast Fourier Transform using Radix-2 algorithm (implemented from scratch)
    • Frequency spectrum visualization
    • Magnitude analysis up to Nyquist frequency
  3. Time-Frequency Analysis (CWT)

    • Continuous Wavelet Transform using Morlet wavelet
    • Interactive scalogram visualization (contour plot)
    • 3D surface plot of CWT coefficients
    • Configurable CWT parameters:
      • Morlet ω₀ parameter
      • Scale parameters (start, step, count)
      • Number of time translations

PCG Component Detection

  • Automated Feature Detection: Identify heart sound components (M1, T1, A2, P2)
  • Binary Thresholding: Adjustable amplitude threshold for region detection
  • Center of Gravity (CoG): Calculate time, frequency, and amplitude for each detected component
  • Component Labeling: Automatic labeling and visualization on scalogram

Installation

Prerequisites

  • Python 3.7 or higher
  • pip package manager

Dependencies

Install the required packages:

pip install streamlit numpy pandas scipy plotly

Or use the requirements file if available:

pip install -r requirements.txt

Usage

Running the Application

  1. Navigate to the project directory:
cd CWT-From-Scratch
  1. Launch the Streamlit app:
streamlit run cwt.py
  1. The application will open in your default web browser (typically at http://localhost:8501)

Using the Application

Option 1: Upload Signal Data

  1. Select "Upload File" in the sidebar
  2. Upload a .txt file with two columns: time and signal values
  3. The sampling frequency will be automatically calculated from the time data
  4. Adjust CWT parameters as needed

Option 2: Generate Synthetic Signal

  1. Select "Generate Sine Wave" in the sidebar
  2. Configure signal parameters:
    • Sampling frequency (Hz)
    • Duration (seconds)
    • Frequency and amplitude for two sine components
    • Noise level
  3. The signal will be generated automatically

Performing CWT Analysis

  1. Adjust CWT parameters in the sidebar:

    • ω₀: Morlet wavelet parameter (default: 2π × 0.849)
    • Start Scale (a): Initial scale value
    • Scale Step (da): Increment for scale values
    • Number of Scales: Resolution in frequency dimension
    • Number of Time-Shifts: Resolution in time dimension
  2. Click "Compute CWT" to perform the analysis

  3. View results:

    • CWT scalogram (contour plot)
    • Binary mask with threshold adjustment
    • Detected PCG components with time, frequency, and amplitude
    • Optional 3D surface plot
    • Scale-to-frequency and translation-to-time mappings

Data Format

Input File Format

The application expects a 2-column space or tab-delimited .txt file:

0.0000    0.1234
0.0010    0.2345
0.0020    0.3456
...

Column 1: Time (seconds)
Column 2: Signal amplitude

Example Data

Sample PCG signal files are included in the data/ directory:

  • Print_13_v2_PCG_RV.txt
  • Print_14_v2_PCG_RV.txt

Technical Details

Algorithms

CWT Implementation

  • Wavelet: Complex Morlet wavelet
  • Method: Direct convolution in time domain
  • Normalization: Energy-preserving normalization
  • Output: Magnitude of complex CWT coefficients

FFT Implementation

  • Algorithm: Radix-2 Cooley-Tukey FFT
  • Padding: Zero-padding to nearest power of 2
  • Scaling: Normalized magnitude spectrum

Shannon Energy

  • Formula: E(t) = -1/N × Σ(x²·log(x² + ε))
  • Parameters: Frame length and hop size based on sampling rate
  • Output: Energy envelope with mean and standard deviation

Performance

  • Caching: CWT results are cached using Streamlit's caching mechanism
  • Progress Tracking: Real-time progress bar during CWT computation
  • Interactive Plots: Plotly-based visualizations with zoom and pan

Applications

This tool is particularly useful for:

  • Cardiac Signal Analysis: PCG (phonocardiogram) heart sound analysis
  • Signal Processing Education: Understanding time-frequency analysis
  • Research: Exploring wavelet transform parameters
  • Feature Extraction: Identifying signal components in time-frequency domain

Algorithm Complexity

  • CWT Computation: O(N × M × K) where:
    • N = number of scales
    • M = number of translations
    • K = signal length
  • FFT Computation: O(N log N) where N is the padded signal length

Limitations

  • Large parameter combinations (high scale/translation counts) may result in long computation times
  • Memory usage scales with the product of scales and translations
  • CWT is computed in the time domain (not optimized with FFT-based convolution)

License

This project is open-source and available for educational and research purposes.

Author

Created as part of signal processing and biomedical engineering research.

Acknowledgments

  • Wavelet theory and Morlet wavelet implementation
  • PCG signal analysis methodology
  • Streamlit framework for interactive web applications

About

A Streamlit app for CWT & FFT signal analysis with PCG feature detection, built entirely from scratch.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages