function [osc_data,fs] = osc_capture_ds2202() ds2000 = visa('ni','USB0::0x1AB1::0x04B0::DS2A142900939::INSTR'); % Create VISA object. ds2000.InputBufferSize = 2e6; fopen(ds2000); fprintf(ds2000,':CHAN1:BWLimit 20M'); % Set BandWidth Limit to 20MHz. fprintf(ds2000,':ACQ:MDEPth 140000'); % Set Memory Depth from several typical values. fs = str2double(query(ds2000,':ACQ:SRATe?')); % Get Sample Rate. %% Setup Waveform fprintf(ds2000,':STOP'); % Stop oscilloscope. fprintf(ds2000,':WAV:SOURce CHAN1'); fprintf(ds2000,':WAV:MODE RAW'); fprintf(ds2000,':WAVeform:FORMat ASCII'); fprintf(ds2000,':WAV:POINts 131072'); % Number of points that must be less than MDEPth. fprintf(ds2000,':WAV:RESet'); fprintf(ds2000,':WAV:BEGin'); pause(5); % Wait the end of the acquisition. %% Get Waveform while true status = query(ds2000,':WAV:STATus?'); pause(1); % Not neccesary, but... [state,rem] = strtok(status,','); len = strtok(sscanf(rem,'%s'),','); if eq(len,'0') break; end if eq(state(1),'I') osc_data = cell2mat(textscan(query(ds2000,':WAV:DATA?'),'%f','delimiter',',')); break; else osc_data = cell2mat(textscan(query(ds2000,':WAV:DATA?'),'%f','delimiter',',')); end end fprintf(ds2000,':WAV:END'); fprintf(ds2000,':RUN'); % Run the oscilloscope. %% Delete instrument object fclose(ds2000); delete(ds2000); clear ds2000;
clear all clc %% Signal Acquisition [s,fs] = osc_capture_ds2202(); % s - signal, fs - sample frequency. %% Setup parameters number_of_harmonics = 11; % Number of harmonics (including the fundamenal)... % to use in the THD calculation. L = length(s); % Length of the signal for FFT computing. It is used only for plot. T = 1/fs; t = (0:L-1)*T; s_ac = s-mean(s); % Remove DC level. It is not neccesary for thd function. s_ac = s_ac/max(abs(s_ac)); % Normalize signal. As above for thd. %% Estimate the THD of the signal [thd_dBc,harmpow,harmfreq] = thd(s_ac,fs,number_of_harmonics); thd_perc = 10^(thd_dBc/20)*100 % Convert dB to Percents and display result. figure(2); thd(s_ac,fs); % Plot the Spectrum of the signal. %% Apply Butterworth filter to signal and estimate THD for filtered signal [lpFilter_b_II, lpFilter_a_II] = butter(2,2*harmfreq(1)/fs,'low'); % Design B-II filter. filtII_s_ac = filter(lpFilter_b_II, lpFilter_a_II, s_ac); % Apply B-II filter to signal. thd_perc_BII = 10^(thd(filtII_s_ac,fs,number_of_harmonics)/20)*100 % Convert dB to Percents and display result. figure(3); thd(filtII_s_ac,fs); % Plot the Spectrum of the B-II filter. [lpFilter_b_IV, lpFilter_a_IV] = butter(4,2*harmfreq(1)/fs,'low'); % Design B-IV filter. filtIV_s_ac = filter(lpFilter_b_IV, lpFilter_a_IV, s_ac); % Apply B-IV filter to signal. thd_perc_BIV = 10^(thd(filtIV_s_ac,fs,number_of_harmonics)/20)*100 % Convert dB to Percents and display result. figure(4); thd(filtIV_s_ac,fs); % Plot the Spectrum of the B-IV filter.
[qty,b5_comport_list,~,b5] = b5_open_test; % Search connected devices. if qty b5.Port = b5_comport_list{1}; % Assign port to first device. fopen(b5); disp(b5_command(b5,'versions',.1)); % Display Versions of MCU and FPGA. disp(b5_command(b5,'sn',.1)); % Display Serial Number. b5_command(b5,'outrfreq 101050',.1); % Set output frequency. b5_command(b5,'outrlvl 0',.1); % Set output level. b5_command(b5,'outimp 600',.1); % Set output impedance. b5_command(b5,'outpath output',.1); % Connect generator to output connector. fclose(b5); % Close port. delete(b5); % Clear buffer. clear b5 % Remove var from memory. else disp('b5-vf is not connected'); delete(b5); % Clear buffer. clear b5 % Remove var from memory. end
%% Window win = tukeywin(length(s_ac),1); % Generate window. % win = 1; % Disable window. s_ac_win = s_ac.*win; % Apply the window. %% FFT nfft = 2^nextpow2(L); %% FFT length fft_s_ac_win = fft(s_ac_win,nfft)/L; f = fs/2*linspace(0,1,nfft/2+1); win_scale = 1/mean(win); %% Normalization the result fft_s_ac_win_lin = 2*win_scale*abs(fft_s_ac_win(1:nfft/2+1)); fft_s_ac_win_log = 20*log10(fft_s_ac_win_lin); %% Fig 1 figure(1); subplot(2,1,1), plot(t,s_ac), axis([0 max(t) -max(abs(s_ac)) max(abs(s_ac))]), grid on; title('\itADC','fontsize',10); xlabel('Time, \its','fontsize',10), ylabel('Amplitude, \itV','fontsize',10); subplot(2,1,2), plot(f,fft_s_ac_win_log), axis([0 fs/2 -110 10]), grid on; title('\itFast Fourier Transform (FFT) Plot','fontsize',10); xlabel('Frequency, \itHz','fontsize',10), ylabel('Amplitude, \itdB','fontsize',10);
Source: https://habr.com/ru/post/310582/
All Articles