Monday, October 23, 2017

[Matlab GUI] Plot UI: Data processing tool

It is import to know how to visualize and analyze numerical data generated after either simulation or testing. With a variety of functions of Matlab, it can be easily implemented to visualize and analyze numerical data. However, not only it is often required to buy additional licenses but also we should know how to use functions. Moreover, raw data often requires processing to change its format or structure to draw plots. 
Plot UI is the generic data processing tool which is designed to alleviate these problems. It offers four plotting modes which can draw plots with variables both from *.mat files and on the workspace of Matlab.

(24 Oct 2017)
Plotting mode
l  Plot
l  FFT (Fast Fourier Transform)
l  PSD (Power Spectral Density)
l  Bode Plot

Line property
l  Color
l  Style
l  Width
l  Marker
l  Delete

Axis property
l  Axis label
l  Range
l  Log/Linear scale
l  Title
It does not support FFT and Bode plot.

Example
1. Bode Plot

2. Plot 

3. FFT

4. PSD


Wednesday, October 18, 2017

[Matlab] PSD (Power Spectral Density)

https://www.mathworks.com/help/signal/ug/power-spectral-density-estimates-using-fft.html

Example
-------------------------------------------
function handles = PSDPlotFnc(hObject,eventdata,handles)

% Load the data
result = handles.result;
% Retrieve x data
xi = get(handles.xVariables,'Value');
t = result{xi,1};
% Retrieve y data
yn = get(handles.VariablesList,'String');
yi = get(handles.VariablesList,'Value');
yn = yn{yi,1}; y = result{yi,1};

% Sampling time Ts and sampling frequency Fs
Ts = t(2) - t(1);
Fs = 1/Ts;

% Data length
N = length(y);

% FFT
ydft = fft(y);
ydft = ydft(1:N/2+1);
psdy = ( 1+(Fs*N) ) * abs(ydft).^2;
psdy(2:end-1) = 2*psdy(2:end-1);
freq = 0:Fs/length(y):Fs/2;

% Plot
pline = plot(handles.axes1,freq,10*log10(psdy),'DisplayName',yn);
hl = legend(handles.axes1,'-DynamicLegend');
set(hl,'interpreter','none');
xlabel(handles.axes1,'Frequency [Hz]');
ylabel(handles.axes1,'Power/Frequency [dB/Hz]');
grid(handles.axes1,'on');


[Matlab] FFT (Fast Fourier Transform)

https://www.mathworks.com/help/matlab/ref/fft.html

Matlab computes DFT(Discrete Fourier Transform) using a FFT algorithm.

Example
-------------------------------------
% Discrete Fourier Transform (DFT)
function handles = DFTPlotFnc(hObject,eventdata,handles)

% Load the data
result = handles.result;
% Retrieve x data
xi = get(handles.xVariables,'Value');
t = result{xi,1};
% Retrieve y data
yn = get(handles.VariablesList,'String');
yi = get(handles.VariablesList,'Value');
yn = yn{yi,1}; y = result{yi,1};

% Sampling time Ts and sampling frequency Fs
Ts = t(2) - t(1);
Fs = 1/Ts;

% Data length L
L = length(y);
% Number of FFT points,
% nextpow2 returns the exponents for the smallest powers of two that satisfy 
nfft = 2^nextpow2(L);

% Frequency
freq = (Fs/2) * linspace(0,1,nfft/2+1);
% Discrete Fourier Transform(DFT)
normalizedFFT = fft(y,nfft)/L; % with zero-padding 

% Plot
pline = plot(handles.axes2,freq,2*abs(normalizedFFT(1:nfft/2+1)),'DisplayName',yn);
dftleg = legend(handles.axes2,'-DynamicLegend');
set(dftleg,'interpreter','none');

xlabel(handles.axes2,'Frequency [Hz]');
ylabel(handles.axes2,'FFT |Y(f)|','interpreter','none');
grid(handles.axes2,'on');

originaline = plot(handles.axes3,t,y,'DisplayName',yn);
orileg = legend(handles.axes3,'-DynamicLegend');
set(orileg,'interpreter','none');
xlabel(handles.axes3,'Time [sec]');
ylabel(handles.axes3,[yn ' Original'],'interpreter','none');
grid(handles.axes3,'on');