Post Go back to editing

Adalm2000 matlab analog example

Hi there, 

I'm enabling one of the analog input channels on my Adalm2000 using the following Matlab code:

%% Setup
import clib.libm2k.libm2k.*
m2k = context.m2kOpen("ip:");
%% Calibrate
%% Set up devices
ain = m2k.getAnalogIn();
%% Enables analog channels
%% Collect analog data
data = ain.getSamplesInterleaved(80000);
data = data.double;
% clib.libm2k.libm2k.context.contextCloseAll();
% clear m2k
However the data plotted is not correct. If I enabled Scopy running in the background I can get the correct input waveform. If I close the  Scopy the waveform is not correct again. What parameters I'm missing in the Matlab code?
Best wishes,

[edited by: kellybritney at 9:02 PM (GMT -4) on 6 Sep 2022]
  • Hi,

    Can you provide some more details about the version of Matlab and libm2k (base and bindings) that you are using?
    I see that you use: "data = ain.getSamplesInterleaved(80000);" and I assume you are using an older libm2k-matlab release, could you confirm?

    This method changed in the latest release and is now called: " data = ain.getSamplesInterleaved_matlab(80000); "
    Do you have the possibility to update to the latest libm2k-matlab release?

    Using an old version of libm2k could cause the issue you are describing. The calibration process was improved in the new versions. This might explain why using Scopy in the background makes it work fine (if Scopy has some newer version).

    Thank you!

  • Hi Alexandra, thanks for the reply. My Matlab is R2022a (installed on Windows) and the toolbox installed is v20.1.1 (which is the latest version available from Matlab, installed by using Matlab add-ons). The analog script is still using getSamplesInterleaved(80000). I've noticed that there is a v20.1.2 from Github which as you said uses getSamplesInterleaved_matlab(80000) (also is said to be compatible with at least R2021a), but the toolbox causes crashes of the Matlab when running the script. Should I downgrade my Matlab to R2020a?


  • Hi,

    We just tested the 20.1.2 installer from Github with Matlab R2022a and it worked fine.

    Could you check the following path : "C:\Users\<your username>\AppData\Roaming\MathWorks\MATLAB Add-Ons\Toolboxes" and let us know if you have any libm2k related directories?

    Did you uninstall the previous 20.1.1 from the Add-On Manager before installing this one? If not, could you uninstall the libm2k Toolbox first and then install the desired version?

    After uninstalling you should not have any directory related to libm2k in the location mentioned above. If you installed 2 separate versions, there might have been 2 directories created there, and the scripts were not using the correct one.

    Thank you!

  • Yes there are multiple copies of the libm2k binding files and I've tried to delete them and reinstall v20.1.2. The rest code is fine, but data = ain.getSamplesInterleaved_matlab(1000) will cause crash of my Matlab. 


  • "ain.getSamplesInterleaved_matlab " should be used for v20.1.0 while " ain.getSamplesInterleaved" would work for all the older versions.

    Can you run " disp(clib.libm2k.libm2k.context.getVersion() " and let us know what version is displayed?


  • Did you install libm2k separately from the Matlab libm2k installer?
    You can check in the System32 directory for libm2k.dll . If you find one, then you probably installed the libm2k library in your system at some point.

    The libm2k Matlab installer takes care of the libm2k.dll and installs it in the Toolboxes directory (with all the necessary dependencies). But if you have libm2k.dll in the system, then maybe Matlab will load that one instead of the one in Toolboxes.

    If that's the case, you can uninstall libm2k from your system.
    Or if you need libm2k in the system, then you should use this installer: to install the correct version required by the Matlab bindings.


  • v20.1.2 requires libm2k v0.5 (while mine was v0.4), so after updating my libm2k the analog example works and the waveform is correct. What did you change to the digital example? I also need to use the digital functions but this time the digital script (my script was working with the older toolbox) causes crash of my Matlab.


  • The digital example did not have any changes for this release.
    Do you have some more details about what's making it crash? The sample acquisition? The ADALM2000 connection?


  • My system also has a FMCOMMS5 and I need to control it as well. I've written an initialization function to first initialize all the SDRs (so later I just need to send and receive buffers to/from SDRs which make the data acquisition much faster). 

    function [tx,rx,dig] = Initilisations(Fs_A2000,Fs_SDR,LO_Rx,LO_Tx,Gain_rx,Gain_tx,buff_rx,A)
    %% Initilisations
    %% Setup for Adalm2000
    import clib.libm2k.libm2k.*
    m2k = clib.libm2k.libm2k.context.m2kOpen("ip:");
    if clibIsNull(m2k)
    m2k = context.m2kOpen();
    if isempty(m2k)
    error('M2K device not found');
    dig = m2k.getDigital();
    N_BITS = 4;
    for k=1:N_BITS
    dig.setSampleRateOut(Fs_A2000); %Setting up sample rate for Adalm20000, up to 100MSPS
    % clib.libm2k.libm2k.context.contextCloseAll();
    % clear m2k
    %% Setup for FMCOMMS5 Tx
    'IPAddress','', ...
    'ChannelMapping',[1 2 3 4],...
    'Gain',Gain_tx,... %-89.75 to 0dB in 0.25dB steps
    'DataSourceSelect','Input port',... %Input Port or DDS
    'DDSTone1Freq',[0 0 0 0],... %range is from 0 to BasebandSampleRate/2
    'DDSTone2Freq',[0 0 0 0],... %range is from 0 to BasebandSampleRate/2
    'DDSTone1Scale',[0 0 0 0],... %range is from 0 to 1
    'DDSTone2Scale',[0 0 0 0],... %range is from 0 to 1
    'DataTimeout',Inf,... %Inf or nonnegative scalar
    'BISTLoopbackMode','Disabled',... %'Digital Tx -> Digital Rx' or 'RF Rx -> RF Tx'
    'BISTToneInject','Disabled',... %'Tone Inject Tx' or 'Tone Inject Rx'
    'BISTToneFreq','Fs/32',... %'Fs_SDR' or 'Fs_SDR*3/32' or 'Fs_SDR/8'
    'BISTToneLevel','0'); %'0', '-6', '-12', '-18'
    %step(tx,data) The input signal data is a colum vector of complex double
    %precision, single precision, or 16 bit integer values
    % designCustomFilter(tx)
    Signal=A*ones(1,1000)+ones(1,1000)*1j*A; % generate a single tone at LO
    transmitRepeat(tx,[Signal' Signal' Signal' Signal']) % Download a waveform to the radio and repeatedly transmit it over the air
    %% Setup for Rx
    'IPAddress','', ...
    'ChannelMapping',[1 2 3 4],...
    'GainSource','Manual',... %'AGC Slow Attack', 'AGC Fast Attack'
    'Gain',Gain_rx,... % -10 to 73dB
    % release(rx);
    This function works with the older toolbox but not with the latest one. Also I can initialize Adalm2000 and FMCOMS5 individually but not sequentially. I was wandering if the new release causes any conflict between different SDRs.