[Date Prev][Date Next][Thread Prev][Thread Next] - [Date Index][Thread Index][Author Index]

Re: software to measure ac voltage



Hi,

On Monday 28 May 2001 12:36, Nick Pugh wrote:
> I am looking software that uses the sound card to measure ac voltage.
> Thanks in advance
> nick k5qxj

What? RMS? How about something like that:

#include <stdio.h, unistd.h, stdlib.h, fcntl.h, sys/types.h, sys/stat.h, 
sys/ioctl.h, math.h, linux/soundcard.h> blablabla...

/* fullscale = 3.0V, adjust or use mixer */
#define CALIB_FS_VOLTAGE 3.0                                                  
              
int main(int argc, char **argv)
{
	signed short buf[44100];
	int fd, param, ret, nr_samples, i;
	double sum, rms_voltage;

	fd = open("/dev/dsp", O_RDONLY);
	if (fd < 0) {
		perror("open");
		exit(255);
	}
	param = 16;
	ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &param);
	if (param != 16) {
		perror("SNDCTL_DSP_SAMPLESIZE");
		exit(255);
	}
	param = 0;
	ioctl(fd, SNDCTL_DSP_STEREO, &param);
	if (param != 0) {
		perror("SNDCTL_DSP_STEREO");
		exit(255);
	}
	param = 44100;
	ioctl(fd, SNDCTL_DSP_SPEED, &param);
	fprintf(stdout, "Samplerate: %d\n\n", param);
	while (1) {
		ret = read(fd, buf, 44100*2);
		nr_samples = ret/2;
		if (nr_samples < 0) {
			perror("read");
			exit(255);
		}
		sum = 0
		for (i=0; i<nr_samples; i++) {
			sum += pow(((float) buf[i] / 32768) * CALIB_FS_VOLTAGE, 2);
		}
		rms_voltage = sqrt(sum / nr_samples);
		fprintf(stdout, "RMS Voltage: %8.4fV\r", rms_voltage);
		fflush(stdout);
	}
}

  -- Jens
----
Via the amsat-bb mailing list at AMSAT.ORG courtesy of AMSAT-NA.
To unsubscribe, send "unsubscribe amsat-bb" to Majordomo@amsat.org



AMSAT Top AMSAT Home