Home > matlab code, Uncategorized, youtube demo code > DFT Basis Function Plots

## DFT Basis Function Plots

Just posting this as I had request for code I used in creating a youtube video on the DFT

```%This a script I used when creating the images for the DFT explained youtube video http://youtu.be/B2iUDBZzBpY
%
% It shows the the result of multiplying a signal being analysed by a set of sinusoidal basis functions (which is how the DFT works)
% The figure produced was edit further before using it in the video
close all;
N = 8; %signals are only 8 samples long
n = 0:N-1;

%specify the magnitudes and phases of the sinusoids (integer cyles over N samples)
mags = [ 0 0 5 0 0 0 ]; %change this to include more sinusoids i.e. change 0 values to non zero
phases = [0 0 0 0 0 0]; %phase values for each of the sinusoids

y_range = [-5 5];
interp_val = 100;
n_vis = 0 : (N)*interp_val-1;
x_vis = zeros(1, (N)*interp_val);
x = zeros(1, N);

for k = 0 : N/2
x = x + mags(k+1)*cos(2*pi*(k)*n/N + phases(k+1)); %samples of signal
x_vis = x_vis + mags(k+1)*cos(2*pi*(k)*n_vis/(N*interp_val) + phases(k+1)); %interpolated sinusoid to aid with visualisation
end

num_rows = N/2+2 +N/2; %number of rows in the subplot
subplot(num_rows,2,1)
plot(x_vis);
hold on
plot(n*interp_val, x,'r.')
hold off

set(gca, 'XTick', 0:interp_val:(N)*interp_val)
set(gca, 'XTickLabel', '')
set(gca, 'Box', 'off')
set(gca, 'YTick', y_range)
ylim(y_range)
%for each of the basis functions
for k = 0 : N/2
basis_function = cos(2*pi*k*n/N); %samples of the cosine basis function
basis_function_vis = cos(2*pi*k*n_vis/(N*interp_val)); %an interpolated version to aid with visualisation

mult_sig = basis_function.*x;
mult_sig_vis = basis_function_vis.*x_vis;

basis_function2 = sin(2*pi*k*n/N); %sine wave basis function
basis_function_vis2 = sin(2*pi*k*n_vis/(N*interp_val));

mult_sig2 = basis_function2.*x;
mult_sig_vis2 = basis_function_vis2.*x_vis;

subplot(num_rows,2,(2*k+2)*2-1)
plot(basis_function_vis)
hold on
plot(n*interp_val,basis_function ,'r.')
hold off
set(gca, 'XTick', 0:interp_val:(N)*interp_val)
label = '';
set(gca, 'XTickLabel', label)
set(gca, 'Box', 'off')
if(k > 0)
subplot(num_rows,2,(2*k+1)*2-1)
plot(basis_function_vis2)
hold on
plot(n*interp_val,basis_function2 ,'r.')
hold off
set(gca, 'XTick', 0:interp_val:(N)*interp_val)
label = '';
set(gca, 'XTickLabel', label)
set(gca, 'Box', 'off')
end

subplot(num_rows,2,(2*k+2)*2)
hold on
plot(mult_sig_vis)
plot(zeros(1, N*interp_val),'k')
plot(n*interp_val,mult_sig ,'r.')
hold off
set(gca, 'XTick', 0:interp_val:(N)*interp_val)
set(gca, 'YTick', y_range)
set(gca, 'XTickLabel', label)
set(gca, 'Box', 'off')
ylim(y_range)
xlim([0 N*interp_val])
ylabel(num2str(round(sum(mult_sig)*100)/100))
if(k > 0)
subplot(num_rows,2,(2*k+1)*2)
hold on
plot(mult_sig_vis2)
plot(zeros(1, N*interp_val),'k')
plot(n*interp_val,mult_sig2 ,'r.')
hold off
set(gca, 'XTick', 0:interp_val:(N)*interp_val)
set(gca, 'XTickLabel', label)
set(gca, 'Box', 'off')
set(gca, 'YTick', y_range)
ylim(y_range)
xlim([0 N*interp_val])
ylabel(num2str(round(sum(mult_sig2)*100)/100))
end
end

```