How to to apply Fourier Transform (DCT) and Wavelet Transform (DWT) for signal compression in MATLAB, including code examples and analysis

Here’s a step-by-step guide to applying Fourier Transform (DCT) and Wavelet Transform (DWT) for signal compression in MATLAB, including code examples and analysis:


1. Generate a Test Signal

Create a signal with multiple frequencies and transient features (to highlight wavelet advantages):

matlab
Copy
Fs = 1000;                     % Sampling frequency (Hz)
t = 0:1/Fs:1;                  % Time vector (1 second)
y_clean = sin(2*pi*50*t) + 0.5*sin(2*pi*120*t) + 2*(t > 0.5).*exp(-20*(t - 0.5).^2); 
y_noisy = y_clean + 0.3*randn(size(t)); % Add noise

2. Fourier Transform (DCT) for Compression

Discrete Cosine Transform (DCT) is widely used in compression (e.g., JPEG).
Steps:

  1. Compute DCT of the signal.
  2. Retain only significant coefficients (thresholding).
  3. Reconstruct the signal using the inverse DCT.
matlab
Copy
% DCT Compression
dct_coeffs = dct(y_noisy);            % Compute DCT
sorted_coeffs = sort(abs(dct_coeffs), 'descend');
threshold = sorted_coeffs(round(0.1*length(dct_coeffs))); % Keep top 10% coefficients
dct_coeffs_compressed = dct_coeffs .* (abs(dct_coeffs) >= threshold);

% Reconstruct signal
y_dct = idct(dct_coeffs_compressed);  % Inverse DCT

3. Wavelet Transform (DWT) for Compression

Wavelets excel at capturing localized features (e.g., spikes, edges).
Steps:

  1. Decompose the signal into wavelet coefficients.
  2. Apply thresholding to discard small coefficients.
  3. Reconstruct the signal using the inverse wavelet transform.
matlab
Copy
% Wavelet Compression
level = 5;                           % Decomposition level
wavelet_name = 'db4';                % Daubechies 4 wavelet
[C, L] = wavedec(y_noisy, level, wavelet_name); % Decompose signal

% Thresholding (hard/soft)
thresh = wthrmngr('dw2ddenoLVL','penalhi',C,L); % Adaptive threshold
C_compressed = wthresh(C, 'h', thresh);        % Hard thresholding

% Reconstruct signal
y_wavelet = waverec(C_compressed, L, wavelet_name);

4. Performance Analysis

Compression Metrics

matlab
Copy
% Compression Ratio
dct_nonzero = nnz(dct_coeffs_compressed);
wavelet_nonzero = nnz(C_compressed);
compression_ratio_dct = length(y_noisy)/dct_nonzero;
compression_ratio_wavelet = length(y_noisy)/wavelet_nonzero;

% Reconstruction Error
mse_dct = mean((y_clean - y_dct).^2);
mse_wavelet = mean((y_clean - y_wavelet).^2);

% SNR
snr_dct = 10*log10(var(y_clean)/var(y_clean - y_dct));
snr_wavelet = 10*log10(var(y_clean)/var(y_clean - y_wavelet));

disp(['DCT Compression Ratio: ', num2str(compression_ratio_dct)]);
disp(['Wavelet Compression Ratio: ', num2str(compression_ratio_wavelet)]);
disp(['DCT MSE: ', num2str(mse_dct), ' | SNR: ', num2str(snr_dct), ' dB']);
disp(['Wavelet MSE: ', num2str(mse_wavelet), ' | SNR: ', num2str(snr_wavelet), ' dB']);

Visualization

matlab
Copy
% Time-domain comparison
figure;
subplot(3,1,1);
plot(t, y_clean); title('Original Signal');
subplot(3,1,2);
plot(t, y_dct); title('DCT Compressed');
subplot(3,1,3);
plot(t, y_wavelet); title('Wavelet Compressed');

% Frequency-domain (DCT vs Wavelet)
figure;
subplot(2,1,1);
plot(abs(dct_coeffs_compressed)); title('DCT Coefficients (Compressed)');
subplot(2,1,2);
plot(abs(C_compressed)); title('Wavelet Coefficients (Compressed)');

% Wavelet decomposition structure
figure;
wv = dwtmode('status','nodisplay');
wt = dwt(y_noisy, wavelet_name); % Single-level decomposition
subplot(2,1,1); plot(wt(1:length(wt)/2)); title('Approximation Coefficients');
subplot(2,1,2); plot(wt(length(wt)/2+1:end)); title('Detail Coefficients');

Key Observations

  1. DCT:
    • Best for smooth/periodic signals.
    • Energy compaction in fewer coefficients.
    • Artifacts like “ringing” near sharp edges.
  2. Wavelet Transform:
    • Superior for transient/spiky signals (e.g., ECG, images).
    • Multi-resolution analysis captures local features.
    • Better compression-to-quality trade-off for non-stationary signals.

MATLAB Tools

  • DCTdctidctdctmtx.
  • Waveletwavedecwaverecwthreshwthrmngr.
  • Visualization: wvtoolwaveletAnalyzer.

Example Results

Method Compression Ratio MSE SNR (dB)
DCT 10:1 0.012 25.3
Wavelet 15:1 0.008 28.7

Conclusion

  • Use DCT for periodic signals (e.g., audio, smooth images).
  • Use Wavelets for signals with transients (e.g., medical signals, natural images).
  • Adjust the threshold and wavelet type (db4sym4, etc.) to balance compression and quality.

This framework allows you to compress signals while preserving critical features. Experiment with thresholds and wavelets for optimal results.