Simple symmetrical wave RMS DEMO using awk.

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Simple symmetrical wave RMS DEMO using awk.
# 1  
Old 08-28-2017
Simple symmetrical wave RMS DEMO using awk.

Hi everyone.
This is DEMO code for getting the RMS value of a symmetrical sampled waveform. It works a treat except see below.
I have used awk as the prime mover as CygWin does not havd 'bc' or 'dc' yet and this is going into AudioScope.sh soon.

However, I hit an anomaly.
In the code below parts B, C, D and G are more than acceptable and E was as expected and also within limits acceptable.
However I never expected this, assume the waveform starts at centreline 0V...
View part F in the code and using just 4 samples with the input waveform being an isosceles triangle then a stepped squarewave becomes the sample.
(The results on editing each waveform are in the code.)
Code:
 Actual waveform.
 ----------------

      +V /\
        /  \
    0V /    \
             \  /
	   -V \/

 Sampled waveform.
 -----------------

     255 +-+
         | |
    128 -+ +-+ +-
             | |
           0 +-+

 RMS results are close to a sinewave using sinewave peak to peak values...
 -------------------------------------------------------------------------

     255 _
        / \
   128 |   |
            \_/
           0

As far as I am concerned there is no way of the sample knowing that the original signal was
either a sinewave or a triangle wave but the RMS result is that of a sinewave.
I can't find anything on a sample size of 4 on a triangle waveform and and its inversion so this anomaly stays.

Bazza.
Code:
#!/bin/bash
# rms.sh
# 'number' is an unsigned decimal integer from 0 to 255, newline delimited.
# 'RMS' the RMS value of a symmetrical wave, sine, square or triangle, (ignore the random one).
# 'Volts_P2P' a derived value from a captured signal, just a number in this demo.
# 'Volts_RMS' the RMS voltage
# 'Volts_P2P', set for a sinewave. Result should be 1.000 or close, (RMS value = 0.707).
# Set for squarewave. Result should be 1.000 or close, (RMS value = 1.000).
# Set for tringlwe wave. Result should be 1.000 or close, (RMS value = 0.577).

# Set a general value for Volts peak to peak.
Volts_P2P=2.000
Volts_RMS=0
waveform="Random"
number=0
RMS=0

# A ------------------------------------- #
# Just a random signal test. Not required.
#waveform="Random"
#for number in range{0..7999}
#do
#	echo "$(( $RANDOM / 127 ))"
#done > "$HOME/sample.txt"

# B ------------------------------------- #
# Crude sinewave test. Results for 8 samples: RMS value = 0.707, RMS Volts = 0.996.
#Volts_P2P=2.818
#waveform="Sinewave"
#echo -n '128
#217
#255
#217
#128
#38
#0
#38' > "$HOME/sample.txt"

# C ------------------------------------- #
# Simple squarewave test. Results for 4 samples: RMS value = 1.004, RMS Volts = 1.004.
#waveform="Squarewave"
#echo -n '255
#255
#0
#0' > "$HOME/sample.txt"

# D ------------------------------------- #
# Fairly accurate triangle wave test. Results for 510 samples: RMS value = 0.580, RMS Volts = 1.005.
#Volts_P2P=3.464
#waveform="Accurate triangle"
#for number in {0..255}
#do
#	echo "$number"
#done > "$HOME/sample.txt"
#for number in {254..1}
#do
#	echo "$number"
#done >> "$HOME/sample.txt"

# E ------------------------------------- #
# Poor triangle wave test, 8 samples. Results for 8 samples: RMS value = 0.616, RMS Volts = 1.067.
#Volts_P2P=3.464
#waveform="Poor triange"
#echo -n '128
#192
#255
#192
#128
#64
#0
#64' > "$HOME/sample.txt"

# F ------------------------------------- #
# Very poor triangle wave test, 4 samples. Results for 4 samples: RMS value = 0.710, RMS Volts = 1.000.
# USE SINEWAVE PEAK TO PEAK VALUE HERE...
Volts_P2P=2.818
waveform="Poor sinewave or very poor triangle"
echo -n '128
255
128
0' > "$HOME/sample.txt"

# G ------------------------------------- #
# Squarewave test, 2 samples only. Results for 2 samples: RMS value = 1.004, RMS Volts = 1.004.
#waveform="Squarewave, 2 samples only"
#echo '255
#0' > "$HOME/sample.txt"

# --------------------------------------- #
# Awk calculation script.
RMS=$( awk 'BEGIN \
{
	# Initialise variables.
	N = 0;
	sum_quares = 0;
}
{
	# Convert 0 to 255 to -128 to 127 and divide by 127.
	# 0 is now the the centreline instead of 128.
	number = ( ( $1 - 128 ) / 127 );
	sum_squares = sum_squares + ( number * number );
	N = N + 1;
}
END \
{
	printf "%.3f", ( sqrt ( sum_squares / N ) );
}' < "$HOME/sample.txt" )

# Do a simple awk conversion to RMS votage knowing the peak to peak value.
Volts_RMS=$( awk -v Volts="$Volts_P2P" -v rms="$RMS" 'BEGIN { printf "%.3f", ( ( Volts / 2 ) * rms ); }' )

echo "$waveform wave: RMS value = $RMS, RMS Volts = $Volts_RMS."

# 2  
Old 08-28-2017
It looks to me like you have a granularity problem. Four dots as a sample can fit a lot of functions, with intermediate datapoints from increased sampling rates you could then use fft to get good sine waves, for example.

You can code fft.c then call it using the system() function in awk. I think your awk supports that call.

A google for 'fft.c' got me 10 first page hits that looked respectably good. There are sites with an array of algorithmic variants to meet differing needs.

fft is gold standard for fitting data sampling output.
# 3  
Old 09-06-2017
Hi JM...
(Thanks for the reply.)
Apologies for any typos, and hope it is lucid enough.
Quote:
Originally Posted by jim mcnamara
It looks to me like you have a granularity problem. Four dots as a sample can fit a lot of functions, with intermediate datapoints from increased sampling rates you could then use fft to get good sine waves, for example.

You can code fft.c then call it using the system() function in awk. I think your awk supports that call.

A google for 'fft.c' got me 10 first page hits that looked respectably good. There are sites with an array of algorithmic variants to meet differing needs.

fft is gold standard for fitting data sampling output.
Apologies for the delay, I have not been around for a while. My triangle wave is more or less hypothetical as sampling a real time random triangle wave that......
1) starts at the 0 point,
2) is initially positive going,
3) has 4 samples, or any number of the same 4 samples,
......is close to impossible.
But a file CAN be so easily be created as shown in the single cycle version in the code.
Although I was aware of the errors that creep in when creating the RMS values I was NOT aware of this one off bizarre situation, (and could find nothing on the WWW about it), so after creating the simple code I made a point of there being a reference to this anomaly.

As the RMS value(s) are near the exact value(s) for that of a sinewave hence my point.

In the days of Debian 6 and PCLinuxOS 2009, (my two main OSes in those days), when '/dev/dsp' was available, recreating the waveforms through '/dev/dsp' would result in a triangle wave as the sound system interpolation was Linear, this was also true for some Windows machines of bygone days. I am not sure what interpolations are used in current OSes and sound systems but it sure looks trigonometric to me, so I would guess that a sinewave would be recreated as a result.

I am happy with the idea of the RMS code at this point but looking more towards True RMS but would probably need to rethink my approach.

But to re-iterate for others, be aware of this anomaly.

LBNL, for any FFT work needed, I would do it through Python and SciPy as the heavy lifting is done already, see previous uploads of AudioScope.sh for the AF Spectrum display...

Bazza...

Last edited by wisecracker; 09-06-2017 at 12:18 PM.. Reason: Modify for error...
# 4  
Old 09-06-2017
Your sample rate is way too low. It's just like trying to mathematically approximate the volume of a teapot by calculating the volume of a cube. Far too approximate to be useful!

When I plot your sine wave data, it looks awfully triangular. The crucial area going through the origin, accounting for the vast majority of the waveform, is a straight line from 38 to 217 -- and being symmetrical, can't be anything but a straight line. There's not enough points to represent anything else.

Mathematically speaking, triangle waves and square waves include higher frequencies than their base frequencies. Without a high enough sample rate to catch them, this information is thrown out like a brutal low pass filter. (Or worse, included as aliasing artifacts.)

Your waveforms should not be 8 points per wave. 16 at bare minimum, probably, but why not 1024? Then you'd see reliable differences, all the time.

Last edited by Corona688; 09-06-2017 at 07:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Programming

Porting Rogue Wave to Linux

I am challenged with porting an old application from Solaris to Red Hat. The application uses Rogue Wave and I am searching for a Red Hat implementation. Your help is appreciated! (2 Replies)
Discussion started by: FunkyWinkerbean
2 Replies

2. Programming

about wave file integrating.

Now I have two wave file(*.wav) at Tru64 Unix machine. I want to make a new wave file including the two wave file. how I should finish this programmer. If you know, can you give me the format of the wave file(*.wav) and Sun au file(*.au). Thank you. (1 Reply)
Discussion started by: livic
1 Replies
Login or Register to Ask a Question