【问题标题】:Setting the Frequency for Analog Output using NI DAQMx使用 NI DAQMx 设置模拟输出的频率
【发布时间】:2014-10-01 18:36:56
【问题描述】:

我正在尝试使用 NI DAQMx ANSI C 库输出包含在 wav 文件中的波形。我正在使用 libsnd 库读取 wav 文件,并且能够成功提取数据,但是输出波形的频率远高于实际的 wav 文件本身。有谁知道如何设置输出波形的频率。我使用的是 PCIe 6351 数据采集卡。

以下是我为完成此任务而编写的一些代码:

#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();
}

如果能得到任何帮助,我将不胜感激。谢谢!

阿图尔

【问题讨论】:

    标签: nidaqmx digital-analog-converter


    【解决方案1】:

    现在,您的程序正在尽可能快地将样本一次写入 DAQ 卡,并且您以 1000 个为一组发送样本。在 DAQmx 术语中,这是一个“软件定时”任务,因为操作系统、调度程序、CPU 和其他系统动态会影响样本写入卡的频率。

    由于音频文件是以恒定速率采样的,您还需要对 DAQ 卡进行编程以以相同速率生成样本。在 DAQmx 术语中,使用采样时钟称为“硬件定时”任务。 DAQmx 还附带用于配置采样时钟的 ANSI C 示例 [1]。看看“Continuously Generate Voltage - Internal Clock”,它可能在磁盘上有一个缩写名称,以及它如何使用函数DAQmxCfgSampClkTiming [2]。还有更多关于在线 DAQmx 计时如何工作的信息 [3]。

    例如,如果您的音频文件以 44.1 kHz 采样,您需要将采样时钟频率设置为相同。 请注意,6351 的时基为 100 MHz [4],并将其除以整数以获得较低的采样时钟速率。所以对于这个 44.1 kHz 的例子,你可以获得的最接近的频率是 44.111 kHz (100 MHz / 2267) 或 44.091 kHz (100 MHz / 2268)。您可以在配置后使用DAQmxGetSampClkRate [5] 检查实际采样率——DAQmx 会将其强制为有效值。

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

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

    [3] 时序、硬件与软件
    http://zone.ni.com/reference/en-XX/help/370466V-01/TOC11.htm

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

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

    【讨论】:

    • 非常感谢乔。这给了我一个继续前进的方向。我需要将此速率设置为音频 wav 文件的采样率吗?当我将简单正弦波输出的函数 DAQmxCfgSampClkTiming 的速率参数更改为所需的频率时。* 1000 我得到正确的输出,但是对于 WAV 文件,这样做会导致比实际波形高得多的频率。你对此有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2016-11-09
    • 2010-11-30
    相关资源
    最近更新 更多