简体   繁体   English

使用NI DAQMx设置模拟输出的频率

[英]Setting the Frequency for Analog Output using NI DAQMx

I'm trying to ouptput a the waveform contained in a wav file using the NI DAQMx ANSI C library. 我正在尝试使用NI DAQMx ANSI C库输出包含在wav文件中的波形。 I'm using the libsnd library to read the wav file and I'm able to extract the data sucessfully, however the frequency of the output waveform is much higher than the actual wav file itself. 我正在使用libsnd库读取wav文件,并且能够成功提取数据,但是输出波形的频率比实际的wav文件本身高得多。 Does anyone know how the frequency of the output waveform can be set. 有谁知道如何设置输出波形的频率。 I'm using the PCIe 6351 Data Acquisition card. 我正在使用PCIe 6351数据采集卡。

Below is some code I wrote out to do this task: 以下是我为执行此任务而写的一些代码:

#include<stdio.h>
#include<conio.h>
#include <math.h>
#include <stdlib.h>
#include <windows.h>
#include "NIDAQmx.h"
#include "Sync_AIAO.h"
#include "sndfile.h"
#include "RIB2.h"


int32 fnCreateTask(TaskHandle *AOTaskHandle)
{
    int32 error=0;
    DAQmxErrChk(DAQmxCreateTask("", AOTaskHandle));
    Error:
    return error;
}






int main(int argc, char** argv)
{
    int i=0;
    int32 error=0;
    TaskHandle  AOtaskHandle = 0;
    float64* AIOSample;
    float *fWavSample;
    SNDFILE *SoundFile;
    SF_INFO SoundFileInfo;  
    int iNoOfSamples=0;
    FILE* fp;
    //Error code
    //Handle to the tasks created
    char        errBuff[2048]={'\0'};


    //DAQmxErrChk(DAQmxCreateTask("",AOtaskHandle));
    fnCreateTask(&AOtaskHandle);

    //Create an analog out channel
    DAQmxErrChk (DAQmxCreateAOVoltageChan(*   (&AOtaskHandle),"Dev1/ao1","",-10.0000000,+10.00000,DAQmx_Val_Volts,NULL));

    //Set for 
  //DAQmxErrChk     (DAQmxCfgDigEdgeStartTrig(&AOtaskHandle,"ai/StartTrigger",DAQmx_Val_Rising));


SoundFile=sf_open("sine.wav", SFM_READ, &SoundFileInfo);

  //Check if file is opened sucessfully
  if (SoundFile == NULL)
  {
    printf("Failed to open the file.\n");
    exit(-1);
  }

  //allocate memory for the buffer that is to hold the wav data:
  fWavSample = new float[SoundFileInfo.channels * SoundFileInfo.frames]; 
  iNoOfSamples = SoundFileInfo.channels * SoundFileInfo.frames;

  //Read data into the float structure
  sf_readf_float(SoundFile, fWavSample, SoundFileInfo.frames);

  printf("Float:%d, Float64:%d\n",sizeof(float),sizeof(float64));

  //printf("%f\n",fWavSample[0]);
  //printf("%f\n",fWavSample[200000]);

  AIOSample = new float64[iNoOfSamples];


 // fopen_s(&fp,"output.dat","w");

  for(i=0;i<SoundFileInfo.channels * SoundFileInfo.frames;i++)
  {
     // fprintf(fp,"Data[%d]:%f\n",i,fWavSample[i]);
      AIOSample[i] = (float64)fWavSample[i];

  }

 // fclose(fp);
  int32 written;


  /*calling function that will output the trigger on PFI6*/

  //fnSrPlayElectric(); //play electric stimulus 
  while(1)
  {
      /*
      DAQmxErrChk(DAQmxWriteAnalogF64(AOtaskHandle,(SoundFileInfo.channels * SoundFileInfo.frames), 
      true, 10.0, DAQmx_Val_GroupByChannel,AIOSample,&written,NULL));
      */

      DAQmxErrChk(DAQmxWriteAnalogF64(AOtaskHandle,1000, 
      true, 10.0, DAQmx_Val_GroupByChannel,AIOSample,&written,NULL));

  //Sleep(3000);
  }





//Display the error to the user here.
Error:
    if( DAQmxFailed(error) )
    {
        DAQmxGetExtendedErrorInfo(errBuff,2048);
        puts(errBuff);
    }




getch();
}

I'd appreciate any help I can get. 我将不胜感激。 Thanks! 谢谢!

Atul 阿图尔

Right now, your program is writing samples to the DAQ card one at a time as fast as the process can and you're sending samples in groups of 1000. In DAQmx terms, this is a "software-timed" task, since the OS, scheduler, CPU, and other system dynamics affect how often a sample is written to the card. 现在,您的程序正在以最快的速度一次将样本写入DAQ卡,并且以1000为一组发送样本。在DAQmx术语中,这是一项“软件定时”任务,因为操作系统,调度程序,CPU和其他系统动力学会影响将样本写入卡的频率。

Since audio files are sampled at a constant rate, you will also need to program the DAQ card to generate the samples at that same rate . 由于音频文件以固定的速率采样,因此您还需要对DAQ卡进行编程,以相同的速率生成采样 In DAQmx terms, using a sample clock is called a "hardware-timed" task. 用DAQmx术语来说,使用采样时钟称为“硬件定时”任务。 DAQmx also comes with ANSI C examples for configuring a sample clock [1]. DAQmx还带有用于配置采样时钟[1]的ANSI C示例。 Take a look at "Continuously Generate Voltage - Internal Clock", which probably has an abbreviated name on disk, and how it uses the function DAQmxCfgSampClkTiming [2]. 看一下“连续生成电压-内部时钟”,它在磁盘上可能有一个缩写名称,以及它如何使用功能DAQmxCfgSampClkTiming [2]。 There is also more information on how timing works for DAQmx online [3]. 还有更多有关DAQmx在线定时工作方式的信息[3]。

For example, if your audio file is sampled at 44.1 kHz, you'll need to set the sample clock frequency to be the same. 例如,如果音频文件以44.1 kHz采样,则需要将采样时钟频率设置为相同。 Beware however, that the 6351 has a 100 MHz timebase [4] and divides it down by integers to get lower sample clock rates. 但是请注意 ,6351具有100 MHz时基[4]并将其除以整数以获得较低的采样时钟速率。 So for this 44.1 kHz example, the closest frequency you can get is 44.111 kHz (100 MHz / 2267) or 44.091 kHz (100 MHz / 2268). 因此,对于这个44.1 kHz的示例,您可以获得的最接近的频率是44.111 kHz(100 MHz / 2267)或44.091 kHz(100 MHz / 2268)。 You can check the actual sample rate using DAQmxGetSampClkRate [5] after you configure it -- DAQmx will coerce it to a valid value. 配置它后,您可以使用DAQmxGetSampClkRate [5]检查实际采样率DAQmxGetSampClkRate会将其强制为有效值。

[1] Text Based NI-DAQmx Data Acquisition Examples :: ANSI C [1]基于文本的NI-DAQmx数据采集示例:: ANSI C
http://www.ni.com/white-paper/6999/en/#ANSIC http://www.ni.com/white-paper/6999/en/#ANSIC

[2] NI-DAQmx C Reference Help :: DAQmxCfgSampClkTiming [2] NI-DAQmx C参考帮助:: DAQmxCfgSampClkTiming
http://zone.ni.com/reference/en-XX/help/370471W-01/daqmxcfunc/daqmxcfgsampclktiming http://zone.ni.com/reference/zh-XX/help/370471W-01/daqmxcfunc/daqmxcfgsampclktiming

[3] Timing, Hardware Versus Software [3]时序,硬件与软件
http://zone.ni.com/reference/en-XX/help/370466V-01/TOC11.htm http://zone.ni.com/reference/zh-XX/help/370466V-01/TOC11.htm

[4] X Series User Manual :: Clock Routing (page 183) [4] X系列用户手册::时钟路由 (第183页)
http://digital.ni.com/manuals.nsf/websearch/82BB2FBF407E178586257D15006F596C http://digital.ni.com/manuals.nsf/websearch/82BB2FBF407E178586257D15006F596C

[5] NI-DAQmx C Reference Help :: DAQmxGetSampClkRate [5] NI-DAQmx C参考帮助:: DAQmxGetSampClkRate
http://zone.ni.com/reference/en-XX/help/370471W-01/mxcprop/func1344 http://zone.ni.com/reference/zh-XX/help/370471W-01/mxcprop/func1344

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM