I have a waveform which contains the phase shift information obtained from RF transceiver. It is not a regular waveform, but instead it is uneven as shown in the picture below. I have illustrate the envelope using paint, and how can I do that in MATLAB?
I also attached the waveform.csv. In my MATLAB code, I already used an averaging filter to smooth out the raw signal.
clc; clear all; close all;
%% Extract data from .csv
file = '150825_px6_human_rotcw_0to60cm_Ch1.csv';
data = csvread(file, 0, 3);
%% Shift time
shift = data(1,1);
for i = 1:length(data)
t(i) = data(i,1) - shift;
end
%% Low pass filter
filterlen = 500;
y = myfilter(data(:,2), filterlen);
%% Plot
figure;
plot(data(:,1), data(:,2));
title('Raw signal');
figure;
plot(t(1:end-filterlen+1), y);
title('After low pass filter');
myfilter.m (simple averaging filter):
function y = myfilter(x,a)
for i = 1:(length(x)-(a-1))
y(i) = sum(x(i:(i+a-1)))/a;
end
y = y';
end
A simple envelope detector can be readily implemented following the idea of a diode detector using:
envelope = data(1,2);
for i = 2:length(data)
if (abs(data(i,2)) > envelope)
envelope = abs(data(i,2));
else
envelope = envelope * decayfactor;
data(i,2) = envelope;
end
end
The decayfactor
should be chosen to have a decay time constant that is much longer than the uneven signal's variation, yet smaller than the reciprocal of the expected signal envelope's bandwidth. You may need to experiment a little bit, but something such as decayfactor=exp(-fmax/fs)
(where fmax
is your envelope bandwidth, and fs
is the sampling frequency) should be a good starting value.
This would typically be followed by a low-pass filter (such as myfilter
which you have provided).
As a sample execution based on the provided data, using decayfactor=exp(-5/5000)
and an average filter with a 300 samples window yields the following plot (with the original data in blue and the envelope in red):