04-11-2012 09:04 AM - edited 04-11-2012 09:07 AM
Hi there,
I just started learning how to control the NI9234 to acquire sound/vibration data.
Based on NI's sample ANSI code, I could control the NI 9234 to read the analog input from the channel connected with PCB microphone. However, if I saved it into a txt file and plotted the txt data in Matlab, the figure looked like random noises other than sound signal (human voices). Code with which I am using, and figure of acquired txt data are shown as below.
May anyone tell me if the code is correct? If yes, what happened to the txt data?
Many thanks in advance for your time and help. Waiting for the reply.
Environment: Visual studio express 2008, win 7 (64 bits)
Figure 1. the whole sound data acquired from NI 9234 (sampling rate 25600 Hz, with time length of 10 seconds)
Figure 2 Zoomin of Figure 1.
Code with which I am using
/********************************************************************* * * ANSI C Example program: * ContAcqSndPressSamps-IntClk.c *********************************************************************/ #include <stdio.h> #include <stdlib.h> #include <NIDAQmx.h> #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData); int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData); int main(void) { int32 error=0; TaskHandle taskHandle=0; char errBuff[2048]={'\0'}; /*********************************************/ // DAQmx Configure Code /*********************************************/ // Note: DSA devices now support including channels from multiple // devices in a single task. DAQmx automatically synchronizes the // devices in such a task. See the DAQmx Help >> Device Considerations >> // Multi Device Tasks section for further details. DAQmxErrChk (DAQmxCreateTask("",&taskHandle)); DAQmxErrChk (DAQmxCreateAIMicrophoneChan(taskHandle,"cDAQ1Mod1/ai1","",DAQmx_Val_PseudoDiff,DAQmx_Val_Pascals,10,120.0,DAQmx_Val_Internal,0.002,NULL)); DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",25600.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,256000)); DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,256000,0,EveryNCallback,NULL)); DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL)); /*********************************************/ // DAQmx Start Code /*********************************************/ DAQmxErrChk (DAQmxStartTask(taskHandle)); printf("Acquiring samples continuously. Press Enter to interrupt\n"); getchar(); Error: if( DAQmxFailed(error) ) DAQmxGetExtendedErrorInfo(errBuff,2048); if( taskHandle!=0 ) { /*********************************************/ // DAQmx Stop Code /*********************************************/ DAQmxStopTask(taskHandle); DAQmxClearTask(taskHandle); } if( DAQmxFailed(error) ) printf("DAQmx Error: %s\n",errBuff); printf("End of program, press Enter key to quit\n"); getchar(); return 0; } int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData) { int32 error=0; char errBuff[2048]={'\0'}; static int totalRead=0; int32 read=0; // float64 data[256000]; float64 *data, *line; FILE *fp; int i; data = (float64*)malloc(nSamples * sizeof(float64)); line = (float64*)malloc(nSamples * sizeof(float64)); /*********************************************/ // DAQmx Read Code /*********************************************/ DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,256000,10.0,DAQmx_Val_GroupByScanNumber,data,256000,&read,NULL)); if( read>0 ) { printf("Acquired %d samples. Total %d\r",read,totalRead+=read); fp = fopen("result.txt", "w"); if(!fp) { printf("create and open file failed\n"); return; }else { printf("file opened successfully\n"); } for (i=0;i<256000;i++) { sprintf(&line[0],"%d\n",data[i]); fputs(line, fp); } fclose(fp); printf("file saved successfully\n"); fflush(stdout); } free(data); Error: if( DAQmxFailed(error) ) { DAQmxGetExtendedErrorInfo(errBuff,2048); /*********************************************/ // DAQmx Stop Code /*********************************************/ DAQmxStopTask(taskHandle); DAQmxClearTask(taskHandle); printf("DAQmx Error: %s\n",errBuff); } return 0; } int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData) { int32 error=0; char errBuff[2048]={'\0'}; // Check to see if an error stopped the task. DAQmxErrChk (status); Error: if( DAQmxFailed(error) ) { DAQmxGetExtendedErrorInfo(errBuff,2048); DAQmxClearTask(taskHandle); printf("DAQmx Error: %s\n",errBuff); } return 0; }
04-12-2012 02:28 AM
Problem solved.
It's my own stupid mistake during the saving session. It should be
sprintf(&line[0],"%f\n",data[i]);
instead of
sprintf(&line[0],"%d\n",data[i]);
cheers