MATLAB Examples

Estimate the Transfer Function of a Circuit using Live Measurements

This example uses MATLAB® to connect to an ADALM1000 source-measurement unit, configure it to generate an arbitrary signal, make live measurements, and use the measurements to calculate the transfer function of the connected circuit.

Contents

Introduction

In this example you have an R-C circuit consisting of a 1 $K\Omega$ resistor in series with a 0.1 $\mu F$ capacitor. The R-C circuit is attached to the ADALM1000 device with Channel A of the device providing the voltage stimulus consisting of a chirp signal. The output of Channel A is connected to the resistor, and the ground is connected to the capacitor. Channel B of the device is used to measure the voltage across the capacitor. The circuit diagram below shows the measurement setup.

In this second part of the example you use the knowledge of the stimulus and the measured waveform to estimate the transfer function of the R-C circuit and compare the measured response to the theoretical response of the R-C circuit.

Discover supported data acquisition devices connected to your system

daq.getDevices
ans = 

Data acquisition devices:

index   Vendor    Device ID                      Description                      
----- ----------- --------- ------------------------------------------------------
1     directsound Audio0    DirectSound Primary Sound Capture Driver
2     directsound Audio1    DirectSound Microphone (Realtek High Definition Audio)
3     directsound Audio2    DirectSound HP 4120 Microphone (HP 4120)
4     directsound Audio3    DirectSound Primary Sound Driver
5     directsound Audio4    DirectSound Speaker/HP (Realtek High Definition Audio)
6     directsound Audio5    DirectSound HP 4120 (HP 4120)
7     adi         SMU1      Analog Devices Inc. ADALM1000

Create a session using the ADALM1000 device

ADISession = daq.createSession('adi')
ADISession = 

Data acquisition session using Analog Devices Inc. hardware:
   Will run for 1 second (100000 scans) at 100000 scans/second.
   No channels have been added.
   

Add voltage source and measurement channels

The ADALM1000 device is capable of sourcing voltage and measuring voltage simultaneously on each of the two channels. Set up the device in this mode.

% Add an analog output channel with device ID SMU1 and channel ID A,
% and set its measurement type to Voltage.
addAnalogOutputChannel(ADISession,'smu1','a','Voltage');

% Add an analog input channel with device ID SMU1 and channel ID B,
% and set its measurement type to Voltage.
addAnalogInputChannel(ADISession,'smu1','b','Voltage');

% Confirm the configuration of the session
ADISession
ADISession = 

Data acquisition session using Analog Devices Inc. hardware:
   No data queued.  Will run at 100000 scans/second.
   Number of channels: 2
      index Type Device Channel   MeasurementType        Range      Name
      ----- ---- ------ ------- ------------------- --------------- ----
      1     ao   SMU1   A       Voltage (SingleEnd) 0 to +5.0 Volts
      2     ai   SMU1   B       Voltage (SingleEnd) 0 to +5.0 Volts
   

Define and visualize a chirp waveform used to excite the circuit

Use a chirp waveform of 1V amplitude ranging in frequency from 20Hz to 20kHz to excite the circuit. The chirp occurs in a period of 1s.

Fs = ADISession.Rate;
T = 0:1/Fs:1;
ExcitationSignal = chirp(T,20,1,20e3,'linear');

% Add a DC offset of 2V to ensure the requested output voltage of the
% device is always above zero volts.
Offset = 2;
ExcitationSignal = ExcitationSignal + Offset;

% Visualize the signal in the time-domain.
figure(1)
plot(T, ExcitationSignal,'Blue')
xlim([0 0.15])
xlabel('Time (s)')
ylabel('Magnitude (V)')
title('Time domain plot of input signal')

% Visualize the signal in the frequency domain.
figure(2)
spectrogram(ExcitationSignal,1024,1000,1024,Fs,'yaxis')
title('Frequency domain view of the the input signal')

Stimulate the circuit and simultaneously measure the frequency response

Queue the data to be sent out by the device on the first channel and measure the voltage across the capacitor on the second channel.

queueOutputData(ADISession,ExcitationSignal');
MeasuredSignal = startForeground(ADISession);

Plot the stimulus and the measured signal

figure(1)
hold on;
plot(T, MeasuredSignal,'Red');
xlim([0 0.15])
ylim([1 3])
title('Time domain plot of stimulus and measured signal')
legend('Excitation Signal','Measured Signal')

figure(3)
spectrogram(MeasuredSignal,1024,1000,1024,Fs,'yaxis')
title('Frequency domain view of the the measured signal')

Calculate the Transfer Function of the circuit

Use knowledge of the measured signal and the stimulus signal to calculate the transfer function of the R-C circuit and plot the magnitude response.

% Remove DC offset before processing.
MeasuredSignal = MeasuredSignal - mean(MeasuredSignal);
ExcitationSignal = ExcitationSignal - Offset;
[TFxy,Freq] = tfestimate(ExcitationSignal, MeasuredSignal,[],[],[],Fs);
Mag = abs(TFxy);

% Compare the estimated transfer function to the theoretical magnitude
% response.
R = 1e3;     % Resistance (Ohms)
C = 0.1e-6;  % Capacitance (Farads)
TFMagTheory = abs(1./(1 + (1i*2*pi*Freq*C*R)));

figure(4);
semilogy(Freq,TFMagTheory,Freq,Mag);
xlim([0 20e3])
xlabel('Frequency (Hz)')
ylim([0.05 1.1])
ylabel('Magnitude')
grid on
legend('Theoretical frequency response','Measured frequency response')
title({'Magnitude response of the theoretical'; 'and estimated transfer function'});

Clear the session

clear ADIsession

Additional Information

This example shows how to use MATLAB to configure and control data acquisition hardware to set up an experiment to characterize an R-C circuit. Functions from Signal Processing Toolbox™ define the stimulus waveform to characterize the frequency response of the circuit. Signal processing functions estimate the transfer function based on measured data and compare the theoretical response of the circuit.

Using functionality in toolboxes such as Data Acquisition Toolbox™ and Instrument Control Toolbox™, MATLAB can connect to, configure, and control hardware to make live measurements, and use the measurements for signal processing, visualization, and algorithm design.

For more information on accessing your hardware from MATLAB, refer to the Hardware Support Catalog.