One thing about the squaring-- If you for instance have a sine wave peaking +/- 1.0, and you square it-- You get a nice pure sine wave of double the original frequency, peaking +1.0 / 0.0.
You could sum the values, take the mean, then square root the mean and get the RMS.
But even if you would like to smooth the envelope some other way such as a lowpass filter-- The squaring has advantage because it hasn't added any "distortion series" of harmonics to the pure sine wave, and the output being double the frequency of the input makes it easier to filter/smooth the envelope with less envelope ripple for a given time-constant of the filter.
For instance if you use a smoothing filter that is -3 dB at 40 Hz and -24 dB at 80 Hz, then a squared 40 Hz sine wave would get filtered at -24 dB, rather than the -3 dB of filtering the original 40 Hz sine. So you could get a smoother envelope without making the envelope response "too slow".
Now, abs() of the signal would also give you double the fundamental frequency at +1.0 / 0.0, but the doubled tone would not be a pure sine wave. It would add lots of high harmonics because of the "pointy" artifacts at the pre-abs zero crossings, so your smoothing filter might need to be more aggressive to adequately filter the additional high harmonics out of the envelope. Depends of what purpose that you need the envelope.
I think squaring would also tend to add some "distortion components" to harmonically rich signals, but suspect there would be fewer added high harmonics compared to abs().
You can get RMS with an IIR lowpass filter (rather than summing the squared values)-- 1. Square each sample 2. Feed each squared sample to the lowpass filter (the lowpass filter performs the equivalent of taking the mean of the squared samples) 3. Square-root the filter output whenever you need to know the current RMS level. When you lower the cutoff frequency (raise the time constant) of the lowpass filter, it raises the number of samples which are "averaged" When you raise the cutoff frequency, it "averages" fewer samples and responds quicker and has higher ripple riding on top of the measurement.
https://www.allaboutcircuits.com/tex...-ac-magnitude/
For instance if you abs() the samples and low-pass filter them (with a long-enough filter time constant relative to the tone frequency), then a sine wave input of +1/- 1.0, would measure about 0.637 at the lowpass filter output.
If you square the samples and low-pass them with the same filter you used with the abs() example above, you would measure 0.5 at the lowpass filter output. That is because the squared sine has been doubled in frequency, cut in half of amplitude, and offset so that the bottom peaks kiss 0.0.
However after you square-root the filter ouput, it will measure 0.707 which is the expected RMS value of the sine wave input. The square->filter->square root outputs RMS, and the abs->filter outputs average level.
Another interesting difference is the time-interaction with the envelope smoother, regardless whether it is an IIR or FIR (sum the samples) smoother.
For instance if you have an IIR lowpass smoother which reaches 63% of the max value in 100 ms after the test signal starts, and the smoother reaches 37% of the max value in 100 ms after the test signal stops-- If you filter the squared values then do the square root, the output will attack to 63% in LESS THAN 100 ms, and the output will release to 37% in LONGER THAN 100 ms.
If you have a sum-the-squared-samples FIR smoother which reaches 50% of max value in 100 ms, for both attack and release, then after the square root it is the same deal-- Attack is now shorter than 100 ms and release is now longer than 100 ms. The square root of 0.5 is 0.707. So after the square root, the attack has reached an 0.707 of max after 100 ms, and obviously the attack reached the 0.5 of max sometime BEFORE 100 ms. Same deal for release-- If the filter reached 0.5 max value after 100 ms, then after the square root that is 0.707, and the 0.5 level is reached sometime AFTER 100 ms.
On the other hand, the same filters applied to abs would give different scaling (0.637 vs 0.707) and the time response of attack and release of the filtering would be different.