📄 rw_adc.c
字号:
/* AD_READ - read an adcfile
*--------------------------------------------------------------*
* HISTORY
* 17-Nov-87 Fil Alleva (faa) at Carnegie-Mellon University
* Changed so that binaries can read and written with out
* regard to byte order problems.
*
* 6-Nov-86 Fil Alleva (faa) at Carnegie-Mellon University
* Changed not to allocate mem if *buf is != 0.
*
* 9-Mar-83 Fil Alleva (faa) at Carnegie-Mellon University
* Modified to read new file format and place the buffer on a page
* boundary.
*
*/
#include "ad.h"
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define SWAPW(x) (((x)<<8) | (0xFF & ((x)>>8)))
#define SWAPL(x) ((((x)<<24)&0xFF000000) | (((x)<<8)&0x00FF0000) | \
(((x)>>8) & 0x0000FF00) | (((x)>>24)&0x000000FF))
/* LITTLE_INDIAN - returns non 0 if this is a little indian machine.
*------------------------------------------------------------*
* DESCRIPTION
* Returns non-zero when this code is compiled and run on
* a machine that formats shorts and integers with the least significant
* byte at address 0. Otherwise it returns 0.
*/
static
little_indian ()
{
char b[4];
register long *l = (long *) b;
*l = 1;
return ((int) b[0]);
}
/* AD_READ - read an adc file
*------------------------------------------------------------*
*/
ad_read (dir, file, buf, head)
char *dir;
char *file;
short *buf;
register ad_head_t *head;
{
register fd;
register short *aptr;
register int do_byte_swap = FALSE;
char fullname[1024];
struct stat fstatb;
int samples_read;
FILE *fp;
int ii;
printf("input data file %s \n", file);
if ((fd = open(file, O_RDONLY, 0)) < 0) {
fprintf (stderr, "ad_read: Couldn't open %s\n", file);
return (-1);
}
printf("after fd=%d size=%d \n", fd, sizeof(ad_head_t));
/*
* read header
*/
if ((fp = fopen(file, "r")) == NULL) {
fprintf(stderr, "Couldn't open datafile %s\n", file);
exit(-1);
}
printf("%d %d fp= %d size=%d \n",
head, sizeof(short), fp, sizeof(ad_head_t)/2);
fread (head, sizeof(short), sizeof(ad_head_t)/2, fp);
printf("head->ad_samples=%d \n", head->ad_samples);
// read(fd, head, sizeof(ad_head_t)/2);
/*
* Check the header size
*/
if (head->ad_hdrsize == 0) {
/*
* There is no header therefore this file must have been written on a
* little indian machine (vax).
*/
head->little_indian = TRUE;
} else {
/*
* There is a header, check the version number.
*/
if (head->ad_version == 0) {
/*
* Version 0 files were only written on VAX's and other little
* indian machines. Later versions of the header have the
* little_indian field.
*/
head->little_indian = TRUE;
}
}
/*
* check head->little_indian and machine type and set do_byte_swap
* accordingly.
*/
if ((head->little_indian && little_indian()) ||
(!head->little_indian && !little_indian()))
do_byte_swap = FALSE;
else
do_byte_swap = TRUE;
if (do_byte_swap) {
/*
* Swap the header bytes
*/
head->ad_hdrsize = SWAPW(head->ad_hdrsize);
head->ad_version = SWAPW(head->ad_version);
head->ad_channels = SWAPW(head->ad_channels);
head->ad_rate = SWAPW(head->ad_rate);
head->ad_samples = SWAPL(head->ad_samples);
head->little_indian = SWAPL(head->little_indian);
}
if ((head->ad_hdrsize > 1024) || (head->ad_hdrsize < 0)) {
fprintf(stderr, "ad_read: file = %s, header size = [%d], bad value\n",
file, head->ad_hdrsize);
close(fd);
return (-1);
}
if (head->ad_hdrsize == 0) {
head->ad_version = -1;
head->ad_channels = 1;
head->ad_rate = 250;
head->ad_samples = 0;
}
/*
* Header or not skip to the begining of the data.
*/
lseek(fd, head->ad_hdrsize * 2, 0);
/*
* Compute the number samples in this file from an fstat if the header is
* inadequit.
*/
if (head->ad_samples == 0) {
if (fstat(fd, &fstatb)) {
fprintf (stderr, "ad_read: could not get file status on [%s]\n",
file);
close (fd);
return (-1);
}
head->ad_samples = (fstatb.st_size / 2) - head->ad_hdrsize;
}
/*
* Use valloc so that the buffer returned can be used by the analog to
* digital converter.
*/
if (!buf) {
aptr = (short *) malloc(head->ad_samples * 2);
if (aptr == 0) {
fprintf (stderr, "ad_read: Memory allocation failed.\n");
close (fd);
return (-1);
}
} else
aptr = buf;
/*
* Read the data all in swell foop.
*/
{
/*
printf("\ntrying to read data\n file %x buf %x num_samples %d\
dir %s file %s\n\n",
fd,aptr,head->ad_samples,dir,file);
*/
samples_read = read(fd, aptr, head->ad_samples << 1) >> 1;
//printf("samples_read=%d, head->ad_samples=%d \n",
// samples_read, head->ad_samples);
//for(ii=0; ii<20; ii++) printf("aptr[%d]=%d \n",ii, aptr[ii]);
if (samples_read < 0) { /* some kind o' read error has occurred */
perror("ad_read");
return -1;
}
if (samples_read != head->ad_samples) {
fprintf (stderr, "ad_read: Premature eof on %s [%d %d]\n",
file, head->ad_samples, samples_read);
close(fd);
return (-1);
}
}
close(fd);
buf = aptr;
//for(ii=200; ii<300; ii++) printf("buf[%d]=%d \n",ii, buf[ii]);
if (do_byte_swap)
swab (aptr, aptr, head->ad_samples*2);
return (0);
}
ad_write (filename, ah, ad_buf)
char *filename;
ad_head_t *ah;
// char *ad_buf;
short *ad_buf;
{
int fd;
int ii;
printf("output data file %s \n", filename);
fd = open (filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
if (fd < 0) {
fprintf (stderr, "ad_write: Couldn't open %s\n", filename);
return (-1);
}
ah->ad_hdrsize = sizeof (ad_head_t) >> 1;
ah->ad_version = CURRENT_AD_VERSION;
ah->little_indian = little_indian();
printf("ah->ad_samples==%d\n",ah->ad_samples);
//for(ii=0; ii<20; ii++) printf("ad_buf[%d]=%d \n",ii, ad_buf[ii]);
write (fd, ah, sizeof (ad_head_t));
write (fd, ad_buf, ah->ad_samples*2);
close (fd);
return (0);
}
/*
main()
{
short buf[102400];
register ad_head_t *head;
int ii;
char *dir = "tmp";
char *file ="/net/alpha4/usr3/msiegler/an4_8k/an4test_clstk/fcaw/an406-fcaw-b.adc";
char *filename ="test.dat";
printf("main");
printf("%s\n",file);
ad_read (dir, file, buf, head);
ad_write (filename, head, buf);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -