# Automate the length of samples in the envelope function to get the optimal peak envelope from various signals

1 view (last 30 days)
Shiva Vikram Bhagavatula on 16 May 2023
Commented: Star Strider on 22 May 2023
The optimal extraction of peak envelope with the 'envelope' function (https://nl.mathworks.com/help/signal/ref/envelope.html) necessitates the optimal selection of the sample length ("The envelopes are determined using spline interpolation over local maxima separated by at least np samples."). While manual optimization of the length is the most common route, it does not allow a rapid and accurate envelope extraction from different types of signals (with different time - frequency relationships). Is there a way to automate the selection of sample length for obtaining the optimal envelope? How to describe the optimal peak envelope (one definition would be touching all the peaks with minimal difference)?
Star Strider on 22 May 2023
This is the sort of approach I had in mind when I wrote my Answer —
clear all
close all
t=0:1e-9:1e-6;% time
fc=1e6; % carrier
del_f=300e3; % frequency deviation
mod_f=100e3; % modulating frequency
std_dev=10; %introduce randomness in amplitude of carrier and frequency deviation
amp=(std_dev*randn(length(t),1))';
mod_signal=1e-2*amp.*(del_f*cos(2*pi*mod_f*t)); % modulating signal with randomized frequency deviation
fmcw_mod=5*amp.*cos(2*pi*fc*t+mod_signal); % fmcw modulated signal
figure
plot(t,fmcw_mod);
factor=2^9
factor = 512
% env_len=round(length(fmcw_mod)/factor);%length over which the function executes
[pks,locs] = findpeaks(fmcw_mod, 'MinPeakProminence',10) % Use 'MinPeakProminence' To Select The Peaks To Detect
pks = 1×287
57.2126 50.1938 119.7309 48.5453 13.4690 22.4986 -1.5904 38.7909 74.1662 29.8850 42.2083 79.4538 17.2369 67.5221 113.0527 2.8986 57.9184 35.6831 29.1597 0.3300 18.3431 22.4078 26.7490 36.7620 8.1993 8.4524 45.4026 55.4935 38.1078 15.8680
locs = 1×287
3 5 9 17 19 21 24 28 32 35 38 45 48 52 57 60 63 65 69 75 77 82 85 90 94 96 101 108 110 113
env_len = floor(median(diff(locs))) % Create An Appropriate Metric From The Returned Peak Indices
env_len = 3
[up,lo]=envelope(fmcw_mod,env_len,'peak');
hold all
plot(t,up);% upper envelope
plot(t,lo);% lower envelope figure
plot(t,fmcw_mod);
hold on
plot(t,up);% upper envelope
plot(t,lo);% lower envelope
hold off
xlim([0.4 0.6]*1E-6)
title('Detail') .

Star Strider on 17 May 2023
Consider first using either findpeaks or islocalmax with find, and then calculate the appropriate distances (using min, max, mean, median or some other metric on the differences of the peak locations) to get the appropriate parameter for the second envelope argument. This is likely easiest, although another option would be to calculate the one-sided fft, find the peaks of the absolute values, and then calculate the inverse of the frequency at that peak location.

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!