Changes to Matlab model
This commit is contained in:
@@ -1,23 +0,0 @@
|
|||||||
dt = 0.01;
|
|
||||||
T = 1;
|
|
||||||
t = [0:dt:T]' ;
|
|
||||||
omega0 = 2 * pi /T;
|
|
||||||
N = length (t);
|
|
||||||
N2 = round (N/2);
|
|
||||||
x = ones(N, 1);
|
|
||||||
x (N2 + 1:N) = -1 * ones(N - N2, 1);
|
|
||||||
a(1) = 1/T * ( sum (x) * dt);
|
|
||||||
xfs = a(1) * ones( size(x));
|
|
||||||
for k = 1:10
|
|
||||||
ck = cos (k * omega0 * t); % cosine component
|
|
||||||
a(k + 1) = 2/T * ( sum (x.* ck) * dt);
|
|
||||||
sk = sin (k * omega0 * t); % sine component
|
|
||||||
b(k + 1) = 2/T * ( sum (x.* sk) * dt);
|
|
||||||
|
|
||||||
% Fourier series approximation
|
|
||||||
xfs = xfs + a(k + 1) * cos (k * omega0 * t) + b(k + 1) * sin (k * omega0 * t);
|
|
||||||
plot (t, x, '-' , t, xfs, ':' );
|
|
||||||
legend ( ' desired ' , ' approximated ' );
|
|
||||||
drawnow ;
|
|
||||||
pause (1);
|
|
||||||
end
|
|
||||||
Binary file not shown.
Binary file not shown.
-294
@@ -1,294 +0,0 @@
|
|||||||
%% FFT algoritm
|
|
||||||
clear; % clear all data from memmory
|
|
||||||
start_time = 0;
|
|
||||||
number_of_samples = 32;
|
|
||||||
end_time = number_of_samples - 1;
|
|
||||||
n = linspace(start_time, end_time , number_of_samples );
|
|
||||||
|
|
||||||
f1 = 2;
|
|
||||||
a1 = 0.2;
|
|
||||||
|
|
||||||
f2 = 2;
|
|
||||||
a2 = 00;
|
|
||||||
|
|
||||||
f3 = 1;
|
|
||||||
a3 = 00;
|
|
||||||
comp1 = a1 * cos( f1 *2*pi*n/number_of_samples);
|
|
||||||
comp2 = a2 * sin( f2 *2*pi*n/number_of_samples);
|
|
||||||
comp3 = a3 * sin( f3 *2*pi*n/number_of_samples);
|
|
||||||
|
|
||||||
data = comp1 + comp2 + comp3;
|
|
||||||
|
|
||||||
|
|
||||||
figure(1)
|
|
||||||
plot (n, comp1, '-');
|
|
||||||
hold on;
|
|
||||||
plot (n, comp2, '-');
|
|
||||||
plot (n, comp3, '-');
|
|
||||||
hold off;
|
|
||||||
|
|
||||||
figure(2)
|
|
||||||
plot (n, data);
|
|
||||||
|
|
||||||
figure(3)
|
|
||||||
X_matlab = fft(data, number_of_samples);
|
|
||||||
stem (n,abs(X_matlab))
|
|
||||||
|
|
||||||
%% My FFT
|
|
||||||
|
|
||||||
% W_N vector calculation
|
|
||||||
% W = zeros(1,number_of_samples); % complex
|
|
||||||
% Wr = zeros(1,number_of_samples); % real
|
|
||||||
% Wi = zeros(1,number_of_samples); % imag
|
|
||||||
% for i = 1 : number_of_samples
|
|
||||||
% W(i) = exp(-j * (i-1) * 2 * pi/ number_of_samples );
|
|
||||||
% Wr(i) = real(W(i));
|
|
||||||
% Wi(i) = imag(W(i));
|
|
||||||
% end
|
|
||||||
|
|
||||||
% reverse bit calulation
|
|
||||||
|
|
||||||
bits = length(dec2bin( number_of_samples - 1 ));
|
|
||||||
rev_bit_dec = zeros(1,number_of_samples);
|
|
||||||
|
|
||||||
for i=1:number_of_samples
|
|
||||||
bin_num = dec2bin(i-1 , bits);
|
|
||||||
rev_bit = [];
|
|
||||||
for k=bits:-1:1
|
|
||||||
rev_bit = [rev_bit , bin_num(k)];
|
|
||||||
end
|
|
||||||
rev_bit_dec(i) = bin2dec(rev_bit) + 1; % add 1 to match Matlab numbering
|
|
||||||
end
|
|
||||||
|
|
||||||
% First stage of FFT
|
|
||||||
|
|
||||||
stage = zeros(bits,number_of_samples);
|
|
||||||
|
|
||||||
for i=1:number_of_samples
|
|
||||||
stage(1,i) = data((i));
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
stage(2,1) = stage(1,1) + stage(1,2);
|
|
||||||
stage(2,2) = stage(1,1) - stage(1,2) ;
|
|
||||||
|
|
||||||
stage(2,3) = (stage(1,3) + stage(1,4)) * exp(-j * 0 * 2 * pi/ 4 );
|
|
||||||
stage(2,4) = (stage(1,3) - stage(1,4)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,5) = stage(1,5) + stage(1,6);
|
|
||||||
stage(2,6) = stage(1,5) - stage(1,6);
|
|
||||||
|
|
||||||
stage(2,7) = (stage(1,7) + stage(1,8)) * exp(-j * 0 * 2 * pi/ 4 );
|
|
||||||
stage(2,8) = (stage(1,7) - stage(1,8)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,9) = stage(1,9) + stage(1,10);
|
|
||||||
stage(2,10) = stage(1,9) - stage(1,10);
|
|
||||||
|
|
||||||
stage(2,11) = (stage(1,11) + stage(1,12)) * exp(-j * 0 * 2 * pi/ 4 );
|
|
||||||
stage(2,12) = (stage(1,11) - stage(1,12)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,13) = stage(1,13) + stage(1,14);
|
|
||||||
stage(2,14) = stage(1,13) - stage(1,14);
|
|
||||||
|
|
||||||
stage(2,15) = (stage(1,15) + stage(1,16)) * exp(-j * 0 * 2 * pi/ 4 );
|
|
||||||
stage(2,16) = (stage(1,15) - stage(1,16)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,17) = stage(1,17) + stage(1,18);
|
|
||||||
stage(2,18) = (stage(1,17) - stage(1,18)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,19) = stage(1,19) + stage(1,20);
|
|
||||||
stage(2,20) = (stage(1,19) - stage(1,20)) * exp(-j * 1 * 2 * pi/ 4 );
|
|
||||||
|
|
||||||
stage(2,21) = stage(1,21) + stage(1,22);
|
|
||||||
stage(2,22) = stage(1,21) - stage(1,22);
|
|
||||||
|
|
||||||
stage(2,23) = stage(1,23) + stage(1,24);
|
|
||||||
stage(2,24) = stage(1,23) - stage(1,24);
|
|
||||||
|
|
||||||
stage(2,25) = stage(1,25) + stage(1,26);
|
|
||||||
stage(2,26) = stage(1,25) - stage(1,26);
|
|
||||||
|
|
||||||
stage(2,27) = stage(1,27) + stage(1,28);
|
|
||||||
stage(2,28) = stage(1,27) - stage(1,28);
|
|
||||||
|
|
||||||
stage(2,29) = stage(1,29) + stage(1,30);
|
|
||||||
stage(2,30) = stage(1,29) - stage(1,30);
|
|
||||||
|
|
||||||
stage(2,31) = stage(1,31) + stage(1,32);
|
|
||||||
stage(2,32) = stage(1,31) - stage(1,32);
|
|
||||||
|
|
||||||
|
|
||||||
% stage,
|
|
||||||
|
|
||||||
% Second stage
|
|
||||||
|
|
||||||
|
|
||||||
stage(2,1) = stage(1,1) + stage(1,3);
|
|
||||||
stage(2,2) = stage(1,2) + stage(1,4);
|
|
||||||
stage(2,3) = stage(1,1) - stage(1,3);
|
|
||||||
stage(2,4) = stage(1,2) - stage(1,4);
|
|
||||||
|
|
||||||
stage(2,5) = (stage(1,5) + stage(1,7)) * exp(-j * 0 * 2 * pi/ 8 );
|
|
||||||
stage(2,6) = (stage(1,6) + stage(1,8)) * exp(-j * 1 * 2 * pi/ 8 );
|
|
||||||
stage(2,7) = (stage(1,5) - stage(1,7)) * exp(-j * 2 * 2 * pi/ 8 );
|
|
||||||
stage(2,8) = (stage(1,6) - stage(1,8)) * exp(-j * 3 * 2 * pi/ 8 );
|
|
||||||
|
|
||||||
stage(2,9) = stage(1,9) + stage(1,11);
|
|
||||||
stage(2,10) = stage(1,10) + stage(1,12);
|
|
||||||
stage(2,11) = stage(1,9) - stage(1,11);
|
|
||||||
stage(2,12) = stage(1,10) - stage(1,12);
|
|
||||||
|
|
||||||
stage(2,13) = (stage(1,13) + stage(1,15)) * exp(-j * 0 * 2 * pi/ 8 );
|
|
||||||
stage(2,14) = (stage(1,14) + stage(1,16)) * exp(-j * 1 * 2 * pi/ 8 );
|
|
||||||
stage(2,15) = (stage(1,13) - stage(1,15)) * exp(-j * 2 * 2 * pi/ 8 );
|
|
||||||
stage(2,16) = (stage(1,14) - stage(1,16)) * exp(-j * 3 * 2 * pi/ 8 );
|
|
||||||
|
|
||||||
stage(2,17) = stage(1,17) + 1 * stage(1,19);
|
|
||||||
stage(2,18) = stage(1,18) + 1 * stage(1,20);
|
|
||||||
stage(2,19) = stage(1,19) - W1(1) * stage(1,17);
|
|
||||||
stage(2,20) = stage(1,20) - W1(2) * stage(1,18);
|
|
||||||
|
|
||||||
stage(2,21) = stage(1,21) + 1 * stage(1,23);
|
|
||||||
stage(2,22) = stage(1,22) + 1 * stage(1,24);
|
|
||||||
stage(2,23) = stage(1,23) - W1(1) * stage(1,21);
|
|
||||||
stage(2,24) = stage(1,24) - W1(2) * stage(1,22);
|
|
||||||
|
|
||||||
stage(2,25) = stage(1,25) + 1 * stage(1,27);
|
|
||||||
stage(2,26) = stage(1,26) + 1 * stage(1,28);
|
|
||||||
stage(2,27) = stage(1,27) - W1(1) * stage(1,25);
|
|
||||||
stage(2,28) = stage(1,28) - W1(2) * stage(1,26);
|
|
||||||
|
|
||||||
stage(2,29) = stage(1,29) + 1 * stage(1,31);
|
|
||||||
stage(2,30) = stage(1,30) + 1 * stage(1,32);
|
|
||||||
stage(2,31) = stage(1,31) - W1(1) * stage(1,29);
|
|
||||||
stage(2,32) = stage(1,32) - W1(2) * stage(1,30);
|
|
||||||
|
|
||||||
% theard stage
|
|
||||||
|
|
||||||
|
|
||||||
stage(3,1) = stage(2,1) + stage(2,5);
|
|
||||||
stage(3,2) = stage(2,2) + stage(2,6);
|
|
||||||
stage(3,3) = stage(2,3) + stage(2,7);
|
|
||||||
stage(3,4) = stage(2,4) + stage(2,8);
|
|
||||||
stage(3,5) = stage(2,1) - stage(2,5);
|
|
||||||
stage(3,6) = stage(2,2) - stage(2,6);
|
|
||||||
stage(3,7) = stage(2,3) - stage(2,7);
|
|
||||||
stage(3,8) = stage(2,4) - stage(2,8);
|
|
||||||
|
|
||||||
stage(3,9) = (stage(2,9) + stage(2,13)) * exp(-j * 0 * 2 * pi/ 16 );
|
|
||||||
stage(3,10) = (stage(2,10) + stage(2,14)) * exp(-j * 1 * 2 * pi/ 16 );
|
|
||||||
stage(3,11) = (stage(2,11) + stage(2,15)) * exp(-j * 2 * 2 * pi/ 16 );
|
|
||||||
stage(3,12) = (stage(2,12) + stage(2,16)) * exp(-j * 3 * 2 * pi/ 16 );
|
|
||||||
stage(3,13) = (stage(2,9) - stage(2,13)) * exp(-j * 4 * 2 * pi/ 16 );
|
|
||||||
stage(3,14) = (stage(2,10) - stage(2,14)) * exp(-j * 5 * 2 * pi/ 16 );
|
|
||||||
stage(3,15) = (stage(2,11) - stage(2,15)) * exp(-j * 6 * 2 * pi/ 16 );
|
|
||||||
stage(3,16) = (stage(2,12) - stage(2,16)) * exp(-j * 7 * 2 * pi/ 16 );
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stage(3,17) = stage(2,17) + W2(1) * stage(2,21);
|
|
||||||
stage(3,18) = stage(2,18) + W2(2) * stage(2,22);
|
|
||||||
stage(3,19) = stage(2,19) + W2(3) * stage(2,23);
|
|
||||||
stage(3,20) = stage(2,20) + W2(4) * stage(2,24);
|
|
||||||
stage(3,21) = stage(2,21) - W2(1) * stage(2,17);
|
|
||||||
stage(3,22) = stage(2,22) - W2(2) * stage(2,18);
|
|
||||||
stage(3,23) = stage(2,23) - W2(3) * stage(2,19);
|
|
||||||
stage(3,24) = stage(2,24) - W2(4) * stage(2,20);
|
|
||||||
|
|
||||||
stage(3,25) = stage(2,25) + W2(1) * stage(2,29);
|
|
||||||
stage(3,26) = stage(2,26) + W2(2) * stage(2,30);
|
|
||||||
stage(3,27) = stage(2,27) + W2(3) * stage(2,31);
|
|
||||||
stage(3,28) = stage(2,28) + W2(4) * stage(2,32);
|
|
||||||
stage(3,29) = stage(2,29) - W2(1) * stage(2,25);
|
|
||||||
stage(3,30) = stage(2,30) - W2(2) * stage(2,26);
|
|
||||||
stage(3,31) = stage(2,31) - W2(3) * stage(2,27);
|
|
||||||
stage(3,32) = stage(2,32) - W2(4) * stage(2,28);
|
|
||||||
|
|
||||||
|
|
||||||
% Fourt stage
|
|
||||||
|
|
||||||
%
|
|
||||||
stage(4,1) = stage(3,1) + stage(3,9);
|
|
||||||
stage(4,2) = stage(3,2) + stage(3,10);
|
|
||||||
stage(4,3) = stage(3,3) + stage(3,11);
|
|
||||||
stage(4,4) = stage(3,4) + stage(3,12);
|
|
||||||
stage(4,5) = stage(3,5) + stage(3,13);
|
|
||||||
stage(4,6) = stage(3,6) + stage(3,14);
|
|
||||||
stage(4,7) = stage(3,7) + stage(3,15);
|
|
||||||
stage(4,8) = stage(3,8) + stage(3,16);
|
|
||||||
stage(4,9) = stage(3,1) - stage(3,9);
|
|
||||||
stage(4,10) = stage(3,2) - stage(3,10);
|
|
||||||
stage(4,11) = stage(3,3) - stage(3,11);
|
|
||||||
stage(4,12) = stage(3,4) - stage(3,12);
|
|
||||||
stage(4,13) = stage(3,5) - stage(3,13);
|
|
||||||
stage(4,14) = stage(3,6) - stage(3,14);
|
|
||||||
stage(4,15) = stage(3,7) - stage(3,15);
|
|
||||||
stage(4,16) = stage(3,8) - stage(3,16);
|
|
||||||
|
|
||||||
stage(4,17) = stage(3,17) + W3(1) * stage(3,25);
|
|
||||||
stage(4,18) = stage(3,18) + W3(2) * stage(3,26);
|
|
||||||
stage(4,19) = stage(3,19) + W3(3) * stage(3,27);
|
|
||||||
stage(4,20) = stage(3,20) + W3(4) * stage(3,28);
|
|
||||||
stage(4,21) = stage(3,21) + W3(5) * stage(3,29);
|
|
||||||
stage(4,22) = stage(3,22) + W3(6) * stage(3,30);
|
|
||||||
stage(4,23) = stage(3,23) + W3(7) * stage(3,31);
|
|
||||||
stage(4,24) = stage(3,24) + W3(8) * stage(3,32);
|
|
||||||
stage(4,25) = stage(3,25) - W3(1) * stage(3,17);
|
|
||||||
stage(4,26) = stage(3,26) - W3(2) * stage(3,18);
|
|
||||||
stage(4,27) = stage(3,27) - W3(3) * stage(3,19);
|
|
||||||
stage(4,28) = stage(3,28) - W3(4) * stage(3,20);
|
|
||||||
stage(4,29) = stage(3,29) - W3(5) * stage(3,21);
|
|
||||||
stage(4,30) = stage(3,30) - W3(6) * stage(3,22);
|
|
||||||
stage(4,31) = stage(3,31) - W3(7) * stage(3,23);
|
|
||||||
stage(4,32) = stage(3,32) - W3(8) * stage(3,24);
|
|
||||||
|
|
||||||
% Fifth stage
|
|
||||||
|
|
||||||
W4 = zeros(1,32); % complex
|
|
||||||
for i = 1 : 32
|
|
||||||
W4(i) = exp(-j * (i-1) * 2 * pi/ 32 );
|
|
||||||
end
|
|
||||||
|
|
||||||
stage(5,1) = stage(4,1) + W4(1) * stage(4,17);
|
|
||||||
stage(5,2) = stage(4,2) + W4(2) * stage(4,18);
|
|
||||||
stage(5,3) = stage(4,3) + W4(3) * stage(4,19);
|
|
||||||
stage(5,4) = stage(4,4) + W4(4) * stage(4,20);
|
|
||||||
stage(5,5) = stage(4,5) + W4(5) * stage(4,21);
|
|
||||||
stage(5,6) = stage(4,6) + W4(6) * stage(4,22);
|
|
||||||
stage(5,7) = stage(4,7) + W4(7) * stage(4,23);
|
|
||||||
stage(5,8) = stage(4,8) + W4(8) * stage(4,24);
|
|
||||||
stage(5,9) = stage(4,9) + W4(9) * stage(4,25);
|
|
||||||
stage(5,10) = stage(4,10) + W4(10) * stage(4,26);
|
|
||||||
stage(5,11) = stage(4,11) + W4(11) * stage(4,27);
|
|
||||||
stage(5,12) = stage(4,12) + W4(12) * stage(4,28);
|
|
||||||
stage(5,13) = stage(4,13) + W4(13) * stage(4,29);
|
|
||||||
stage(5,14) = stage(4,14) + W4(14) * stage(4,30);
|
|
||||||
stage(5,15) = stage(4,15) + W4(15) * stage(4,31);
|
|
||||||
stage(5,16) = stage(4,16) + W4(16) * stage(4,32);
|
|
||||||
stage(5,17) = stage(4,17) - W4(1) * stage(4,1);
|
|
||||||
stage(5,18) = stage(4,18) - W4(2) * stage(4,2);
|
|
||||||
stage(5,19) = stage(4,19) - W4(3) * stage(4,3);
|
|
||||||
stage(5,20) = stage(4,20) - W4(4) * stage(4,4);
|
|
||||||
stage(5,21) = stage(4,21) - W4(5) * stage(4,5);
|
|
||||||
stage(5,22) = stage(4,22) - W4(6) * stage(4,6);
|
|
||||||
stage(5,23) = stage(4,23) - W4(7) * stage(4,7);
|
|
||||||
stage(5,24) = stage(4,24) - W4(8) * stage(4,8);
|
|
||||||
stage(5,25) = stage(4,25) - W4(9) * stage(4,9);
|
|
||||||
stage(5,26) = stage(4,26) - W4(10) * stage(4,10);
|
|
||||||
stage(5,27) = stage(4,27) - W4(11) * stage(4,11);
|
|
||||||
stage(5,28) = stage(4,28) - W4(12) * stage(4,12);
|
|
||||||
stage(5,29) = stage(4,29) - W4(13) * stage(4,13);
|
|
||||||
stage(5,30) = stage(4,30) - W4(14) * stage(4,14);
|
|
||||||
stage(5,31) = stage(4,31) - W4(15) * stage(4,15);
|
|
||||||
stage(5,32) = stage(4,32) - W4(16) * stage(4,16);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
figure(4)
|
|
||||||
stem(n, abs( stage(bits,:) ) )
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-510
@@ -1,510 +0,0 @@
|
|||||||
%% FFT algoritm
|
|
||||||
clear; % clears all previus values from memory
|
|
||||||
clc; % clear command window
|
|
||||||
fs = 44100; % samplinf freq.
|
|
||||||
fftLength=512; % windowlength
|
|
||||||
% signal frequencies
|
|
||||||
max = 2048 - 1 ;
|
|
||||||
|
|
||||||
f1 = 430;
|
|
||||||
a1 = 0;
|
|
||||||
|
|
||||||
f2 = 8000;
|
|
||||||
a2 = 0;
|
|
||||||
|
|
||||||
f3 = 8000;
|
|
||||||
a3 = max/2;
|
|
||||||
|
|
||||||
% calculating signals
|
|
||||||
comp1 = a1 * sin(2*pi*f1*[0:1/fs:1]);
|
|
||||||
comp2 = a2 * sin(2*pi*f2*[0:1/fs:1]);
|
|
||||||
comp3 = a3 * sin(2*pi*f3*[0:1/fs:1]);
|
|
||||||
|
|
||||||
% calculatin vector values for step function
|
|
||||||
d1 = ones(1, 24);
|
|
||||||
d2 = 0.*ones(1, 1000 );
|
|
||||||
|
|
||||||
%data = [ d1 , d2]; % creates vector with step function
|
|
||||||
data = comp1 + comp2 + comp3; % creates vector from 3 sin functions
|
|
||||||
|
|
||||||
figure(1) % plots separete sin functions
|
|
||||||
plot ( comp1, '-');
|
|
||||||
hold on;
|
|
||||||
plot ( comp2, '-');
|
|
||||||
plot ( comp3, '-');
|
|
||||||
xlim([1 50])
|
|
||||||
title('Separete SIN functions')
|
|
||||||
ylabel('magnitude'), xlabel('time')
|
|
||||||
hold off;
|
|
||||||
|
|
||||||
figure(2) % plots signal for fft
|
|
||||||
plot ( data);
|
|
||||||
title('Signal for FFT analysis FFT')
|
|
||||||
ylabel('magnitude'), xlabel('time')
|
|
||||||
xlim([1 100])
|
|
||||||
|
|
||||||
figure(3) % plots resultinf fft from Matlab functions
|
|
||||||
ft =fft(data,fftLength);
|
|
||||||
%ftMag=abs(ft(1:fftLength/2));
|
|
||||||
ftMag=abs(ft);
|
|
||||||
plot (ftMag)
|
|
||||||
title('Linear Magnitude FFT')
|
|
||||||
ylabel('magnitude'), xlabel('kHz')
|
|
||||||
|
|
||||||
xt = xticks; % returns the current x-axis tick values as a vector
|
|
||||||
fstep = fs/fftLength; % tick of f axis in f domain
|
|
||||||
xtnew = round(xt*fstep/1000, 1) ; % calculate new tick in kHz
|
|
||||||
xticklabels(xtnew) % set new tick labels
|
|
||||||
|
|
||||||
figure(4) % plots resultinf fft(in dB) from Matlab functions
|
|
||||||
ft =fft(data,fftLength);
|
|
||||||
%ftMag=abs(ft(1:fftLength/2));
|
|
||||||
ftMag=abs(ft);
|
|
||||||
plot (20*log10(ftMag))
|
|
||||||
title('dB Magnitude')
|
|
||||||
ylabel('dB'), xlabel('kHz')
|
|
||||||
|
|
||||||
xt = xticks; % returns the current x-axis tick values as a vector
|
|
||||||
fstep = fs/fftLength; % tick of f axis in f domain
|
|
||||||
xtnew = round(xt*fstep/1000, 1) ; % calculate new tick in kHz
|
|
||||||
xticklabels(xtnew) % set new tick labels
|
|
||||||
|
|
||||||
%% Data preparation for FFT
|
|
||||||
|
|
||||||
% reverse bit calulation
|
|
||||||
bits = length(dec2bin( fftLength - 1 )); % how many bits in binary number
|
|
||||||
rev_bit_dec = zeros(1,fftLength); % create vektor size of fftlength
|
|
||||||
|
|
||||||
for i=1:fftLength
|
|
||||||
bin_num = dec2bin(i-1 , bits); % converting to binary number
|
|
||||||
rev_bit = []; % create empty vector
|
|
||||||
for k=bits:-1:1
|
|
||||||
rev_bit = [rev_bit , bin_num(k)];
|
|
||||||
end
|
|
||||||
rev_bit_dec(i) = bin2dec(rev_bit) + 1; % add 1 to match Matlab numbering
|
|
||||||
end
|
|
||||||
|
|
||||||
% creating array
|
|
||||||
|
|
||||||
real_n = zeros(bits+1,fftLength); % create empty array to store values in reverse bit order
|
|
||||||
imag_n = zeros(bits+1,fftLength);
|
|
||||||
stage = zeros(bits+1,fftLength);
|
|
||||||
%sfi_data = sfi(data,16,0);
|
|
||||||
for i=1:fftLength
|
|
||||||
real_n(1,i) = data(rev_bit_dec(i)+1);
|
|
||||||
stage(1,i) = data(rev_bit_dec(i)+1);
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
% % W_N vector calculation
|
|
||||||
% W = zeros(1,fftLength); % complex
|
|
||||||
% Wr = zeros(1,fftLength); % real
|
|
||||||
% Wi = zeros(1,fftLength); % imag
|
|
||||||
% for i = 1 : fftLength
|
|
||||||
% W(i) = exp(-j * (i-1) * 2 * pi/ fftLength );
|
|
||||||
% Wr(i) = sfi(real(W(i)),16,15);
|
|
||||||
% Wi(i) = sfi(imag(W(i)),16,15);
|
|
||||||
% end
|
|
||||||
%
|
|
||||||
% % W(30) = - W(30+256)
|
|
||||||
% % or
|
|
||||||
% % W(x) = - W(x + fftLength/2)
|
|
||||||
|
|
||||||
% new W_N vector calculation this time only half
|
|
||||||
|
|
||||||
W = zeros(1,fftLength/2); % complex
|
|
||||||
Wr = zeros(1,fftLength/2); % real
|
|
||||||
Wi = zeros(1,fftLength/2); % imag
|
|
||||||
for i = 1 : fftLength/2
|
|
||||||
W(i) = exp(-j * (i-1) * 2 * pi/ fftLength );
|
|
||||||
Wr(i) = real(W(i));%sfi(real(W(i)),16,15);
|
|
||||||
Wi(i) = imag(W(i));%sfi(imag(W(i)),16,15);
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
%% FFT FSM
|
|
||||||
|
|
||||||
%% First stage
|
|
||||||
|
|
||||||
for i = 1 : 2^1 : fftLength
|
|
||||||
% % % Even
|
|
||||||
% % stage(2,i) = stage(1,i) + stage(1,i+1);
|
|
||||||
% % % Odd
|
|
||||||
% % stage(2,i+1) = stage(1,i) - stage(1,i+1);
|
|
||||||
|
|
||||||
% Even
|
|
||||||
real_n(2,i) = real_n(1,i) + real_n(1,i+1);
|
|
||||||
% Odd
|
|
||||||
real_n(2,i+1) = real_n(1,i) - real_n(1,i+1);
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%% Second stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 2
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 4 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^2 : fftLength
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(3,i+0) = stage(2,i+0) + Wn(1)*stage(2,i+2);
|
|
||||||
% % stage(3,i+1) = stage(2,i+1) + Wn(2)*stage(2,i+3);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(3,i+2) = stage(2,i+0) - Wn(1)*stage(2,i+2);
|
|
||||||
% % stage(3,i+3) = stage(2,i+1) - Wn(2)*stage(2,i+3);
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 1:bits) == '11' % Odd pair odd number(every fourth)
|
|
||||||
|
|
||||||
imag_n(2,i) = -real_n(2,i);
|
|
||||||
real_n(2,i) = 0;
|
|
||||||
% c= real_n(2,i) + j * imag_n(2,i),
|
|
||||||
end
|
|
||||||
end
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^2 : fftLength
|
|
||||||
% Even pair
|
|
||||||
real_n(3,i+0) = real_n(2,i+0) + real_n(2,i+2);
|
|
||||||
real_n(3,i+1) = real_n(2,i+1) + real_n(2,i+3);
|
|
||||||
imag_n(3,i+0) = imag_n(2,i+0) + imag_n(2,i+2);
|
|
||||||
imag_n(3,i+1) = imag_n(2,i+1) + imag_n(2,i+3);
|
|
||||||
% Odd par
|
|
||||||
real_n(3,i+2) = real_n(2,i+0) - real_n(2,i+2);
|
|
||||||
real_n(3,i+3) = real_n(2,i+1) - real_n(2,i+3);
|
|
||||||
imag_n(3,i+2) = imag_n(2,i+0) - imag_n(2,i+2);
|
|
||||||
imag_n(3,i+3) = imag_n(2,i+1) - imag_n(2,i+3);
|
|
||||||
end
|
|
||||||
|
|
||||||
%% Therd stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 4
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 8 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^3 : fftLength
|
|
||||||
% % for k = 0 : 3
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(4,i+k) = stage(3,i+k) + Wn(k+1)*stage(3,i+k+4);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(4,i+k+4) = stage(3,i+k) - Wn(k+1)*stage(3,i+k+4);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 2) == '1' %
|
|
||||||
if i_bin(bits - 1: bits) == '00'
|
|
||||||
% real_n(3,i) = real_n(3,i);
|
|
||||||
% imag_n(3,i) = imag_n(3,i);
|
|
||||||
end
|
|
||||||
if i_bin(bits - 1: bits) == '01'
|
|
||||||
real_x = real_n(3,i)*Wr(65) - imag_n(3,i)*Wi(65);
|
|
||||||
imag_x = real_n(3,i)*Wi(65) + Wr(65)*imag_n(3,i);
|
|
||||||
real_n(3,i) = real_x;
|
|
||||||
imag_n(3,i) = imag_x;
|
|
||||||
end
|
|
||||||
if i_bin(bits - 1: bits) == '10'
|
|
||||||
real_x = real_n(3,i)*Wr(129) - imag_n(3,i)*Wi(129);
|
|
||||||
imag_x = real_n(3,i)*Wi(129) + Wr(129)*imag_n(3,i);
|
|
||||||
real_n(3,i) = real_x;
|
|
||||||
imag_n(3,i) = imag_x;
|
|
||||||
end
|
|
||||||
if i_bin(bits - 1: bits) == '11'
|
|
||||||
real_x = real_n(3,i)*Wr(193) - imag_n(3,i)*Wi(193);
|
|
||||||
imag_x = real_n(3,i)*Wi(193) + Wr(193)*imag_n(3,i);
|
|
||||||
real_n(3,i) = real_x;
|
|
||||||
imag_n(3,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^3 : fftLength
|
|
||||||
for k = 0 : 3
|
|
||||||
% Even pair
|
|
||||||
real_n(4,i+k) = real_n(3,i+k) + real_n(3,i+k+4);
|
|
||||||
imag_n(4,i+k) = imag_n(3,i+k) + imag_n(3,i+k+4);
|
|
||||||
% Odd par
|
|
||||||
real_n(4,i+k+4) = real_n(3,i+k) - real_n(3,i+k+4);
|
|
||||||
imag_n(4,i+k+4) = imag_n(3,i+k) - imag_n(3,i+k+4);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 4th stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 8
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 16 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^4 : fftLength
|
|
||||||
% % for k = 0 : 7
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(5,i+k) = stage(4,i+k) + Wn(k+1)*stage(4,i+k+8);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(5,i+k+8) = stage(4,i+k) - Wn(k+1)*stage(4,i+k+8);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 3) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 2:bits)); % converting last 3 bits to decimal
|
|
||||||
real_x = real_n(4,i)*Wr(n*32+1) - imag_n(4,i)*Wi(n*32+1);
|
|
||||||
imag_x = real_n(4,i)*Wi(n*32+1) + imag_n(4,i)*Wr(n*32+1);
|
|
||||||
real_n(4,i) = real_x;
|
|
||||||
imag_n(4,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^4 : fftLength
|
|
||||||
for k = 0 : 7
|
|
||||||
%Even pair
|
|
||||||
real_n(5,i+k) = real_n(4,i+k) + real_n(4,i+k+8);
|
|
||||||
imag_n(5,i+k) = imag_n(4,i+k) + imag_n(4,i+k+8);
|
|
||||||
%Odd par
|
|
||||||
real_n(5,i+k+8) = real_n(4,i+k) - real_n(4,i+k+8);
|
|
||||||
imag_n(5,i+k+8) = imag_n(4,i+k) - imag_n(4,i+k+8);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 5th stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 16
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 32 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^5 : fftLength
|
|
||||||
% % for k = 0 : 15
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(6,i+k) = stage(5,i+k) + Wn(k+1)*stage(5,i+k+16);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(6,i+k+16) = stage(5,i+k) - Wn(k+1)*stage(5,i+k+16);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 4) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 3:bits)); % converting last 4 bits to decimal
|
|
||||||
real_x = real_n(5,i)*Wr(n*16+1) - imag_n(5,i)*Wi(n*16+1);
|
|
||||||
imag_x = real_n(5,i)*Wi(n*16+1) + imag_n(5,i)*Wr(n*16+1);
|
|
||||||
real_n(5,i) = real_x;
|
|
||||||
imag_n(5,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^5 : fftLength
|
|
||||||
for k = 0 : 15
|
|
||||||
% Even pair
|
|
||||||
real_n(6,i+k) = real_n(5,i+k) + real_n(5,i+k+16);
|
|
||||||
imag_n(6,i+k) = imag_n(5,i+k) + imag_n(5,i+k+16);
|
|
||||||
% Odd par
|
|
||||||
real_n(6,i+k+16)= real_n(5,i+k) - real_n(5,i+k+16);
|
|
||||||
imag_n(6,i+k+16)= imag_n(5,i+k) - imag_n(5,i+k+16);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 6th stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 32
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 64 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^6 : fftLength
|
|
||||||
% % for k = 0 : 31
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(7,i+k) = stage(6,i+k) + Wn(k+1)*stage(6,i+k+32);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(7,i+k+32) = stage(6,i+k) - Wn(k+1)*stage(6,i+k+32);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 5) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 4:bits)); % converting last 5 bits to decimal
|
|
||||||
real_x = real_n(6,i)*Wr(n*8+1) - imag_n(6,i)*Wi(n*8+1);
|
|
||||||
imag_x = real_n(6,i)*Wi(n*8+1) + imag_n(6,i)*Wr(n*8+1);
|
|
||||||
real_n(6,i) = real_x;
|
|
||||||
imag_n(6,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^6 : fftLength
|
|
||||||
for k = 0 : 31
|
|
||||||
% Even pair
|
|
||||||
real_n(7,i+k) = real_n(6,i+k) + real_n(6,i+k+32);
|
|
||||||
imag_n(7,i+k) = imag_n(6,i+k) + imag_n(6,i+k+32);
|
|
||||||
% Odd par
|
|
||||||
real_n(7,i+k+32)= real_n(6,i+k) - real_n(6,i+k+32);
|
|
||||||
imag_n(7,i+k+32)= imag_n(6,i+k) - imag_n(6,i+k+32);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 7th stage
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 64
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 128 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^7 : fftLength
|
|
||||||
% % for k = 0 : 63
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(8,i+k) = stage(7,i+k) + Wn(k+1)*stage(7,i+k+64);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(8,i+k+64) = stage(7,i+k) - Wn(k+1)*stage(7,i+k+64);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 6) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 5:bits)); % converting last 6 bits to decimal
|
|
||||||
real_x = real_n(7,i)*Wr(n*4+1) - imag_n(7,i)*Wi(n*4+1);
|
|
||||||
imag_x = real_n(7,i)*Wi(n*4+1) + imag_n(7,i)*Wr(n*4+1);
|
|
||||||
real_n(7,i) = real_x;
|
|
||||||
imag_n(7,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^7 : fftLength
|
|
||||||
for k = 0 : 63
|
|
||||||
% Even pair
|
|
||||||
real_n(8,i+k) = real_n(7,i+k) + real_n(7,i+k+64);
|
|
||||||
imag_n(8,i+k) = imag_n(7,i+k) + imag_n(7,i+k+64);
|
|
||||||
% Odd par
|
|
||||||
real_n(8,i+k+64)= real_n(7,i+k) - real_n(7,i+k+64);
|
|
||||||
imag_n(8,i+k+64)= imag_n(7,i+k) - imag_n(7,i+k+64);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 8th stage
|
|
||||||
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 128
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 256 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^8 : fftLength
|
|
||||||
% % for k = 0 : 127
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(9,i+k) = stage(8,i+k) + Wn(k+1)*stage(8,i+k+128);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(9,i+k+128) = stage(8,i+k) - Wn(k+1)*stage(8,i+k+128);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 7) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 6:bits)); % converting last 7 bits to decimal
|
|
||||||
real_x = real_n(8,i)*Wr(n*2+1) - imag_n(8,i)*Wi(n*2+1);
|
|
||||||
imag_x = real_n(8,i)*Wi(n*2+1) + imag_n(8,i)*Wr(n*2+1);
|
|
||||||
real_n(8,i) = real_x;
|
|
||||||
imag_n(8,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
for i = 1 : 2^8 : fftLength
|
|
||||||
for k = 0 : 127
|
|
||||||
% Even pair
|
|
||||||
real_n(9,i+k) = real_n(8,i+k) + real_n(8,i+k+128);
|
|
||||||
imag_n(9,i+k) = imag_n(8,i+k) + imag_n(8,i+k+128);
|
|
||||||
% Odd par
|
|
||||||
real_n(9,i+k+128)= real_n(8,i+k) - real_n(8,i+k+128);
|
|
||||||
imag_n(9,i+k+128)= imag_n(8,i+k) - imag_n(8,i+k+128);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% 9th stage
|
|
||||||
|
|
||||||
|
|
||||||
% % % Calculating W twiddling factor
|
|
||||||
% % for i = 1 : 256
|
|
||||||
% % Wn(i) = exp(-j * (i-1) * 2 * pi/ 512 );
|
|
||||||
% % end
|
|
||||||
% %
|
|
||||||
% % % calculate next stage values
|
|
||||||
% % for i = 1 : 2^9 : fftLength
|
|
||||||
% % for k = 0 : 255
|
|
||||||
% % % Even pair
|
|
||||||
% % stage(10,i+k) = stage(9,i+k) + Wn(k+1)*stage(9,i+k+256);
|
|
||||||
% % % Odd par
|
|
||||||
% % stage(10,i+k+256) = stage(9,i+k) - Wn(k+1)*stage(9,i+k+256);
|
|
||||||
% % end
|
|
||||||
% % end
|
|
||||||
|
|
||||||
% Multiply odd pairs with W twiddling factor
|
|
||||||
for i = 1 : 1 : fftLength
|
|
||||||
i_bin = dec2bin(i-1, bits); % calculates "i" in binary
|
|
||||||
|
|
||||||
if i_bin(bits - 8) == '1' %
|
|
||||||
n = bin2dec(i_bin(bits - 7:bits)); % converting last 8 bits to decimal
|
|
||||||
real_x = real_n(8,i)*Wr(n*1+1) - imag_n(8,i)*Wi(n*1+1);
|
|
||||||
imag_x = real_n(8,i)*Wi(n*1+1) + imag_n(8,i)*Wr(n*1+1);
|
|
||||||
real_n(8,i) = real_x;
|
|
||||||
imag_n(8,i) = imag_x;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% calculate next stage values
|
|
||||||
i = 1;
|
|
||||||
for k = 0 : 255
|
|
||||||
% Even pair
|
|
||||||
real_n(10,i+k) = real_n(9,i+k) + real_n(9,i+k+255);
|
|
||||||
imag_n(10,i+k) = imag_n(9,i+k) + imag_n(9,i+k+255);
|
|
||||||
% Odd par
|
|
||||||
real_n(10,i+k+255)= real_n(9,i+k) - real_n(9,i+k+255);
|
|
||||||
imag_n(10,i+k+255)= imag_n(9,i+k) - imag_n(9,i+k+255);
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%% Ploting out
|
|
||||||
% slowly plot result
|
|
||||||
figure(5)
|
|
||||||
for i = bits : bits
|
|
||||||
plot( abs( real_n(i, :) + j.*imag_n(i, :) ) );
|
|
||||||
% plot( abs( stage(i,:) ) );
|
|
||||||
% pause(1);
|
|
||||||
end
|
|
||||||
xt = xticks; % returns the current x-axis tick values as a vector
|
|
||||||
fstep = fs/fftLength; % tick of f axis in f domain
|
|
||||||
xtnew = round(xt*fstep)/1000 ; % calculate new tick in kHz
|
|
||||||
xticklabels(xtnew) % set new tick labels
|
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
%% FFT algoritm
|
|
||||||
clear; % clears all previus values from memory
|
|
||||||
clc; % clear command window
|
|
||||||
fs = 44100; % samplinf freq.
|
|
||||||
fftLength=512; % windowlength
|
|
||||||
stage_num = log2(fftLength);
|
|
||||||
% signal frequencies
|
|
||||||
max = 2048 - 1 ;
|
|
||||||
|
|
||||||
f1 = 430;
|
|
||||||
a1 = 0;
|
|
||||||
|
|
||||||
f2 = 4300;
|
|
||||||
a2 = 0;
|
|
||||||
|
|
||||||
f3 = 8000;
|
|
||||||
a3 = max/2;
|
|
||||||
|
|
||||||
% calculating signals
|
|
||||||
comp1 = a1 * sin(2*pi*f1*[0:1/fs:1]);
|
|
||||||
comp2 = a2 * sin(2*pi*f2*[0:1/fs:1]);
|
|
||||||
comp3 = a3 * sin(2*pi*f3*[0:1/fs:1]);
|
|
||||||
Length = length(comp3);
|
|
||||||
% calculatin vector values for step function
|
|
||||||
d1 = ones(1, 24);
|
|
||||||
d2 = 0.*ones(1, 1000 );
|
|
||||||
|
|
||||||
%data = [ d1 , d2]; % creates vector with step function
|
|
||||||
data = comp1 + comp2 + comp3; % creates vector from 3 sin functions
|
|
||||||
%data = comp3;
|
|
||||||
|
|
||||||
% Grafika nobiides
|
|
||||||
bin_vals = [0 : fftLength-1];
|
|
||||||
N_2 = ceil(fftLength/2);
|
|
||||||
fax_kHz = (bin_vals-N_2)*fs/fftLength/1000;
|
|
||||||
|
|
||||||
freq3 = ceil(-(fftLength)/2:1:(fftLength)/2).*(fs/fftLength)/1000;
|
|
||||||
|
|
||||||
figure(1) % plots separete sin functions
|
|
||||||
hold off,
|
|
||||||
%plot ( comp1, '-');
|
|
||||||
hold on;
|
|
||||||
%plot ( comp2, '-');
|
|
||||||
plot ( comp3, '-'), grid minor,;
|
|
||||||
%xlim([1 50])
|
|
||||||
title('Separete SIN functions')
|
|
||||||
ylabel('magnitude'), xlabel('time')
|
|
||||||
hold off;
|
|
||||||
|
|
||||||
figure(2) % plots signal for fft
|
|
||||||
plot ( data), grid minor,;
|
|
||||||
xlim([1 50])
|
|
||||||
title('Signal for FFT analysis FFT')
|
|
||||||
ylabel('magnitude'), xlabel('time')
|
|
||||||
%xlim([1 100])
|
|
||||||
|
|
||||||
figure(3) % plots resultinf fft from Matlab functions
|
|
||||||
ft =fft(data,fftLength);
|
|
||||||
ft1 = fftshift(ft);
|
|
||||||
ftMag = abs(ft1);
|
|
||||||
plot (fax_kHz,ftMag), grid minor,
|
|
||||||
title('Linear Magnitude FFT')
|
|
||||||
ylabel('magnitude'), xlabel('kHz')
|
|
||||||
|
|
||||||
figure(4) % plots resultinf fft(in dB) from Matlab functions
|
|
||||||
ft = fft(data,fftLength+1);
|
|
||||||
ftMag = abs(ft(1:fftLength+1));
|
|
||||||
plot (freq3,20*log10(ftMag)), grid minor,
|
|
||||||
title('dB Magnitude')
|
|
||||||
ylabel('dB'), xlabel('kHz')
|
|
||||||
|
|
||||||
%% Data preparation for FFT
|
|
||||||
|
|
||||||
% reverse bit calulation
|
|
||||||
bits = length(dec2bin( fftLength - 1 )); % how many bits in binary number
|
|
||||||
rev_bit_dec = zeros(1,fftLength); % create vektor size of fftlength
|
|
||||||
|
|
||||||
stage = 1; %Do it here for stage #1
|
|
||||||
c = 0:fftLength-1;
|
|
||||||
c_bin = de2bi(c); % create binary table
|
|
||||||
rev_bit_dec = bi2de(fliplr(circshift(c_bin',stage-1)')); %Rotate binary table and convert to dec
|
|
||||||
|
|
||||||
% creating array
|
|
||||||
% create empty array to store values in reverse bit order
|
|
||||||
stage = zeros(bits+1,fftLength);
|
|
||||||
real_n = zeros(bits+1,fftLength);
|
|
||||||
imag_n = zeros(bits+1,fftLength);
|
|
||||||
|
|
||||||
Wn = zeros(1,fftLength/2); % complex
|
|
||||||
Wr = zeros(1,fftLength/2); % real
|
|
||||||
Wi = zeros(1,fftLength/2); % imag
|
|
||||||
|
|
||||||
%% New stages
|
|
||||||
|
|
||||||
for st = 0 : stage_num;
|
|
||||||
if st == 0
|
|
||||||
for tmp=1:fftLength;
|
|
||||||
stage(st+1,tmp) = data(rev_bit_dec(tmp)+1);
|
|
||||||
real_n(st+1,tmp) = data(rev_bit_dec(tmp)+1);
|
|
||||||
end
|
|
||||||
else st > 0;
|
|
||||||
for n = 1 : fftLength/2;
|
|
||||||
Wn(n) = exp(-j * (n-1) * 2 * pi/ 2^(st) );
|
|
||||||
Wr(n) = real(Wn(n));
|
|
||||||
Wi(n) = imag(Wn(n));
|
|
||||||
end
|
|
||||||
for i = 1 : 2^st : fftLength;
|
|
||||||
for k = 0 : 2^(st-1)-1;
|
|
||||||
% Even
|
|
||||||
stage(st+1,i+k) = stage(st,i+k) + Wn(k+1)*stage(st,i+k+2^(st-1));
|
|
||||||
real_n(st+1,i+k) = real_n(st,i+k) + Wn(k+1)*real_n(st,i+k+2^(st-1));
|
|
||||||
imag_n(st+1,i+k) = imag_n(st,i+k) + Wn(k+1)*imag_n(st,i+k+2^(st-1));
|
|
||||||
% Odd
|
|
||||||
stage(st+1,i+k+2^(st-1)) = stage(st,i+k) - Wn(k+1)*stage(st,i+k+2^(st-1));
|
|
||||||
real_n(st+1,i+k+2^(st-1)) = real_n(st,i+k) - Wn(k+1)*real_n(st,i+k+2^(st-1));
|
|
||||||
imag_n(st+1,i+k+2^(st-1)) = imag_n(st,i+k) - Wn(k+1)*imag_n(st,i+k+2^(st-1));
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% Ploting out
|
|
||||||
% slowly plot result
|
|
||||||
figure(5)
|
|
||||||
for i = 1 : bits + 1;
|
|
||||||
%plot( abs( real_n(i, :) + j.*imag_n(i, :) ) );
|
|
||||||
%plot( fax_kHz, abs( fftshift( real_n(i, :) + j.*imag_n(i,:) ) ) ), grid minor,;
|
|
||||||
plot( fax_kHz, abs( fftshift( stage(i,:) ) ) ), grid minor,;
|
|
||||||
%pause(1);
|
|
||||||
end
|
|
||||||
|
|
||||||
title('Linear Magnitude FFT')
|
|
||||||
ylabel('magnitude'), xlabel('kHz')
|
|
||||||
|
|
||||||
figure(6)
|
|
||||||
for i = 1 : bits + 1;
|
|
||||||
%plot( abs( real_n(i, :) + j.*imag_n(i, :) ) );
|
|
||||||
plot( fax_kHz, abs( fftshift( real_n(i, :) + j.*imag_n(i,:) ) ) ), grid minor,;
|
|
||||||
%plot( fax_kHz, abs( fftshift( stage(i,:) ) ) ), grid minor,;
|
|
||||||
%pause(1);
|
|
||||||
end
|
|
||||||
|
|
||||||
title('Linear Magnitude FFT, ploted from Real + Imag')
|
|
||||||
ylabel('magnitude'), xlabel('kHz')
|
|
||||||
+65
-92
@@ -5,75 +5,51 @@
|
|||||||
clear; % clears all previus values from memory
|
clear; % clears all previus values from memory
|
||||||
clc; % clear command window
|
clc; % clear command window
|
||||||
fs = 44100; % samplinf freq.
|
fs = 44100; % samplinf freq.
|
||||||
fftLength=256; % windowlength
|
fftLength=2^9; % windowlength
|
||||||
stage_num = log2(fftLength);
|
stage_num = log2(fftLength);
|
||||||
|
Wn_word = 12; % signed fixed point lenght for Wn (fraction is word-2)
|
||||||
|
samp_word = 8; % word lenght of samped signal (fraction is word-2)
|
||||||
|
w_bits = 10; % signed fixed point integer bit lenght
|
||||||
|
f_bits = 10; % signed fixed point integer bit lenght for calculations
|
||||||
|
|
||||||
while 1 % Checking for correct "fftLength"-Wondow length value
|
|
||||||
if ~mod(stage_num,1)==0
|
|
||||||
error('"fftLength"-Wondow length value must be a numer: 2^x= : 2, 4, 8, 16, 32,...');
|
|
||||||
break
|
|
||||||
else
|
|
||||||
% continue working if value is correct
|
|
||||||
% signal frequencies
|
|
||||||
max = 2048 - 1 ;
|
|
||||||
|
|
||||||
f1 = 430;
|
% reading input audio file
|
||||||
a1 = 0;
|
% audio samples are from matlab examples
|
||||||
|
% load handel.mat
|
||||||
f2 = 4300;
|
% filename = 'handel.wav';
|
||||||
a2 = 0;
|
% load gong.mat;
|
||||||
|
filename = 'gong.wav';
|
||||||
f3 = 8000;
|
% audiowrite(filename,y,Fs);
|
||||||
a3 = max/2;
|
[y,fs] = audioread(filename);
|
||||||
|
data = sfi(y, samp_word, samp_word-2);
|
||||||
% calculating signals
|
m = 7; % alow select section of signal for FFT
|
||||||
comp1 = a1 * sin(2*pi*f1*[0:1/fs:1]);
|
data_cut = data(fftLength*m+1:fftLength*m+fftLength);
|
||||||
comp2 = a2 * sin(2*pi*f2*[0:1/fs:1]);
|
%sound(data.double,fs);
|
||||||
comp3 = a3 * sin(2*pi*f3*[0:1/fs:1]);
|
|
||||||
Length = length(comp3);
|
|
||||||
|
|
||||||
data = comp1 + comp2 + comp3; % creates vector from 3 sin functions
|
|
||||||
%data = comp3;
|
|
||||||
|
|
||||||
% Plot shifting to center
|
|
||||||
bin_vals = [0 : fftLength-1];
|
|
||||||
N_2 = ceil(fftLength/2);
|
|
||||||
fax_kHz = (bin_vals-N_2)*fs/fftLength/1000;
|
|
||||||
|
|
||||||
freq3 = ceil(-(fftLength)/2:1:(fftLength)/2).*(fs/fftLength)/1000;
|
|
||||||
|
|
||||||
figure(1) % plots separete sin functions
|
|
||||||
hold off,
|
|
||||||
%plot ( comp1, '-');
|
|
||||||
hold on;
|
|
||||||
%plot ( comp2, '-');
|
|
||||||
plot (comp3, '-')
|
|
||||||
xlim([1 50]), grid minor,;
|
|
||||||
title('Separete SIN functions')
|
|
||||||
ylabel('magnitude'), xlabel('time')
|
|
||||||
hold off;
|
|
||||||
|
|
||||||
figure(2) % plots signal for fft
|
figure(2) % plots signal for fft
|
||||||
plot (data), grid minor,;
|
plot (data_cut), grid minor,
|
||||||
xlim([1 50])
|
% xlim([fftLength*m+1 fftLength*m+fftLength])
|
||||||
title('Signal for FFT analysis FFT')
|
title('Signal for FFT analysis FFT')
|
||||||
ylabel('magnitude'), xlabel('time')
|
ylabel('magnitude'), xlabel('time')
|
||||||
|
|
||||||
|
|
||||||
|
bin_vals = [0 : fftLength-1];
|
||||||
|
|
||||||
figure(3) % plots resultinf fft from Matlab functions
|
figure(3) % plots resultinf fft from Matlab functions
|
||||||
ft = fft(data,fftLength);
|
ft = fft(data_cut.double,fftLength);
|
||||||
ft1 = fftshift(ft);
|
ft1 = fftshift(ft);
|
||||||
ftMag = abs(ft1);
|
ftMag = abs(ft1);
|
||||||
plot (fax_kHz,ftMag), grid minor,
|
plot (bin_vals,ftMag), grid minor,
|
||||||
title('Linear Magnitude FFT')
|
title('Linear Magnitude FFT')
|
||||||
ylabel('magnitude'), xlabel('kHz')
|
ylabel('magnitude'), xlabel(' ')
|
||||||
|
|
||||||
figure(4) % plots resultinf fft(in dB) from Matlab functions
|
% figure(4) % plots resultinf fft(in dB) from Matlab functions
|
||||||
ft = fft(data,fftLength);
|
% ft = fft(data.double,fftLength);
|
||||||
ft1 = fftshift(ft);
|
% ft1 = fftshift(ft);
|
||||||
ftMag = abs(ft1(1:fftLength));
|
% ftMag = abs(ft1(1:fftLength));
|
||||||
plot (fax_kHz,20*log10(ftMag)), grid minor,
|
% plot (bin_vals,20*log10(ftMag)), grid minor,
|
||||||
title('dB Magnitude')
|
% title('dB Magnitude')
|
||||||
ylabel('dB'), xlabel('kHz')
|
% ylabel('dB'), xlabel(' ')
|
||||||
|
|
||||||
%% Data preparation for FFT
|
%% Data preparation for FFT
|
||||||
|
|
||||||
@@ -97,67 +73,64 @@ imag_n_sfi = zeros(bits+1,fftLength);
|
|||||||
|
|
||||||
%% Starting stages
|
%% Starting stages
|
||||||
|
|
||||||
for st = 0 : stage_num;
|
for st = 0 : stage_num
|
||||||
if st == 0
|
if st == 0
|
||||||
for tmp=1:fftLength;
|
for tmp=1:fftLength
|
||||||
stage(st+1,tmp) = data(rev_bit_dec(tmp)+1);
|
stage(st+1,tmp) = data_cut(rev_bit_dec(tmp)+1);
|
||||||
real_n(st+1,tmp) = data(rev_bit_dec(tmp)+1);
|
real_n(st+1,tmp) = data_cut(rev_bit_dec(tmp)+1);
|
||||||
|
real_n_sfi(st+1,tmp) = sfi(real_n(st+1,tmp),f_bits + w_bits ,f_bits);
|
||||||
end
|
end
|
||||||
else st > 0;
|
else st > 0;
|
||||||
for n = 1 : fftLength/2;
|
for n = 1 : fftLength/2
|
||||||
Wn(n) = exp(-j * (n-1) * 2 * pi/ 2^(st) );
|
Wn(n) = exp(-j * (n-1) * 2 * pi/ 2^(st) );
|
||||||
Wr(n) = real(Wn(n));
|
% Wr(n) = real(Wn(n));
|
||||||
Wi(n) = imag(Wn(n));
|
% Wi(n) = imag(Wn(n));
|
||||||
|
Wr(n) = sfi(real(Wn(n)),Wn_word,Wn_word-2);
|
||||||
|
Wi(n) = sfi(imag(Wn(n)),Wn_word,Wn_word-2);
|
||||||
end
|
end
|
||||||
for i = 1 : 2^st : fftLength;
|
for i = 1 : 2^st : fftLength
|
||||||
for k = 0 : 2^(st-1)-1;
|
for k = 0 : 2^(st-1)-1
|
||||||
% Even
|
% Even
|
||||||
stage(st+1,i+k) = stage(st,i+k) + Wn(k+1)*stage(st,i+k+2^(st-1));
|
stage(st+1,i+k) = stage(st,i+k) + Wn(k+1)*stage(st,i+k+2^(st-1));
|
||||||
real_n(st+1,i+k) = real_n(st,i+k) + Wr(k+1)*real_n(st,i+k+2^(st-1)) - Wi(k+1)*imag_n(st,i+k+2^(st-1));
|
real_n(st+1,i+k) = real_n_sfi(st,i+k) + Wr(k+1)*real_n_sfi(st,i+k+2^(st-1)) - Wi(k+1)*imag_n_sfi(st,i+k+2^(st-1));
|
||||||
imag_n(st+1,i+k) = imag_n(st,i+k) + Wi(k+1)*real_n(st,i+k+2^(st-1)) + + Wr(k+1)*imag_n(st,i+k+2^(st-1));
|
imag_n(st+1,i+k) = imag_n_sfi(st,i+k) + Wi(k+1)*real_n_sfi(st,i+k+2^(st-1)) + Wr(k+1)*imag_n_sfi(st,i+k+2^(st-1));
|
||||||
|
real_n_sfi(st+1,i+k) = sfi(real_n(st+1,i+k),f_bits + w_bits,f_bits);
|
||||||
|
imag_n_sfi(st+1,i+k) = sfi(imag_n(st+1,i+k),f_bits + w_bits,f_bits);
|
||||||
% Odd
|
% Odd
|
||||||
stage(st+1,i+k+2^(st-1)) = stage(st,i+k) - Wn(k+1)*stage(st,i+k+2^(st-1));
|
stage(st+1,i+k+2^(st-1)) = stage(st,i+k) - Wn(k+1)*stage(st,i+k+2^(st-1));
|
||||||
real_n(st+1,i+k+2^(st-1)) = real_n(st,i+k) - Wr(k+1)*real_n(st,i+k+2^(st-1)) + Wi(k+1)*imag_n(st,i+k+2^(st-1));
|
real_n(st+1,i+k+2^(st-1)) = real_n_sfi(st,i+k) - Wr(k+1)*real_n_sfi(st,i+k+2^(st-1)) + Wi(k+1)*imag_n_sfi(st,i+k+2^(st-1));
|
||||||
imag_n(st+1,i+k+2^(st-1)) = imag_n(st,i+k) - Wi(k+1)*real_n(st,i+k+2^(st-1)) - Wr(k+1)*imag_n(st,i+k+2^(st-1));
|
imag_n(st+1,i+k+2^(st-1)) = imag_n_sfi(st,i+k) - Wi(k+1)*real_n_sfi(st,i+k+2^(st-1)) - Wr(k+1)*imag_n_sfi(st,i+k+2^(st-1));
|
||||||
|
real_n_sfi(st+1,i+k+2^(st-1)) = sfi(real_n(st+1,i+k+2^(st-1)),f_bits + w_bits,f_bits);
|
||||||
|
imag_n_sfi(st+1,i+k+2^(st-1)) = sfi(imag_n(st+1,i+k+2^(st-1)),f_bits + w_bits,f_bits);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
%% Constructing signed fixed-point numeric objects
|
|
||||||
|
|
||||||
for n = 1 : fftLength/2;
|
|
||||||
Wr_sfi(n) = sfi(real(Wn(n)),16);
|
|
||||||
Wi_sfi(n) = sfi(imag(Wn(n)),16);
|
|
||||||
end
|
|
||||||
|
|
||||||
for n = 1 : fftLength;
|
|
||||||
for k = 1 : st + 1
|
|
||||||
real_n_sfi(k,n) = sfi(real_n(k,n),24);
|
|
||||||
imag_n_sfi(k,n) = sfi(imag_n(k,n),24);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
%% Plotting out
|
%% Plotting out
|
||||||
|
|
||||||
% for slowly result plotting uncomment pause
|
% for slowly result plotting uncomment pause
|
||||||
|
|
||||||
figure(5)
|
figure(5)
|
||||||
for i = 1 : bits + 1;
|
%for i = 1 : bits + 1;
|
||||||
|
for i = bits+1 : bits + 1
|
||||||
%plot( fax_kHz, abs( fftshift( real_n(i, :) + j.*imag_n(i,:) ) ) ),
|
%plot( fax_kHz, abs( fftshift( real_n(i, :) + j.*imag_n(i,:) ) ) ),
|
||||||
plot( fax_kHz, abs( fftshift( stage(i,:) ) ) ),
|
plot( bin_vals, abs( fftshift( stage(i,:) ) ) ),
|
||||||
grid minor, title('Linear Magnitude FFT'), ylabel('magnitude'), xlabel('kHz');
|
grid minor, title('Linear Magnitude FFT'), ylabel('magnitude'), xlabel(' ');
|
||||||
%pause(1);
|
%pause(1);
|
||||||
end
|
end
|
||||||
|
|
||||||
figure(6)
|
figure(6)
|
||||||
for i = 1 : bits + 1;
|
%for i = 1 : bits + 1;
|
||||||
plot( fax_kHz, abs( fftshift( real_n(i, :) + j.*imag_n(i,:) ) ) ),
|
for i = bits+1 : bits + 1
|
||||||
grid minor, title('Linear Magnitude FFT, ploted from Real + Imag'), ylabel('magnitude'), xlabel('kHz');
|
plot( bin_vals, abs( fftshift( real_n_sfi(i, :) + j.*imag_n_sfi(i,:) ) ) ),
|
||||||
|
grid minor, title('Linear Magnitude FFT, ploted from Real + Imag'), ylabel('magnitude'), xlabel(' ');
|
||||||
%plot( fax_kHz, abs( fftshift( stage(i,:) ) ) ), grid minor,;
|
%plot( fax_kHz, abs( fftshift( stage(i,:) ) ) ), grid minor,;
|
||||||
%pause(1);
|
%pause(1);
|
||||||
end
|
end
|
||||||
|
|
||||||
break
|
figure(7)
|
||||||
end
|
dif = abs( fftshift( stage(bits+1,:) ) ) - abs( fftshift( real_n_sfi(bits+1, :) + j.*imag_n_sfi(bits+1,:) ) ) ;
|
||||||
end
|
plot( bin_vals, dif )
|
||||||
|
grid minor, title('Difference in plots'), ylabel('diff magnitude'), xlabel(' ');
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ io_module #(
|
|||||||
.sclk_ws_ratio(sclk_ws_ratio), //number of sclk periods per word select period
|
.sclk_ws_ratio(sclk_ws_ratio), //number of sclk periods per word select period
|
||||||
.d_width(d_width) //data width
|
.d_width(d_width) //data width
|
||||||
) io_module (
|
) io_module (
|
||||||
//.reset_n(reset_n), //asynchronous active high reset
|
.reset(reset_n), //asynchronous active high reset
|
||||||
.mclk(master_clk), //master clock
|
.mclk(master_clk), //master clock
|
||||||
.da_sclk(da_sclk), //serial clock (or bit clock)
|
.da_sclk(da_sclk), //serial clock (or bit clock)
|
||||||
.da_ws(da_lrck), //word select (or left-right clock)
|
.da_ws(da_lrck), //word select (or left-right clock)
|
||||||
|
|||||||
Reference in New Issue
Block a user