own code, loop form to vector form

1 回表示 (過去 30 日間)
Stalin García Ruano
Stalin García Ruano 2021 年 8 月 15 日
hello, I have my own code to obtain the fft, a vector x that corresponds to a signal in time domain is entered, and the fft returns me, this function uses loops, I need to have the same function but without loops, that is, transforming the iterators of the loops in vectors or matrices.I have tried in various ways but I have not succeeded. somebody could help me?
the code is the following:
this fft code is from radix 2 method.
function [ y ] = my_fft_for( x )
N1 = length(x); % calculates the size of the Xn sequence
nFFT = 2^ceil(log2(N1)); % calculates the number of samples to complete
x =[x zeros(1,nFFT-N1)]; % The sequence is completed with 0's to nFFT elements
N=length(x); % calculates the number of samples of xn
b=bin2dec(fliplr(dec2bin (0:1:nFFT-1)))+1; % Reordering of samples (reverse bit)
x=x(b); % Signal reordered
S=log2(N); % calculates the number of stages
Half=1; % Initialize with half value
for stage=1:S % FOR loop of stages of the algorithm Log2 (N)
for index=0:(2^stage):(N-1) % Butterflies for each stage of the algorithm
for n=0:(Half-1) % A butterfly is calculated and the result is saved
pos=n+index+1; % Index of samples
pow=(2^(S-stage))*n; % Power of complex multiplication
w=exp((-1i)*(2*pi)*pow/N); % Complex multiplication
a=x(pos)+x(pos+Half).*w; % First part of the butterfly
b=x(pos)-x(pos+Half).*w; % Second part of the butterfly
x(pos)=a; % Saving result of the first part of the butterfly
x(pos+Half)=b; % Saving result of the second part of the butterfly
end
end
Half=2*Half; % Calculating the next "Half" value
end
y=x; % The result of the function is saved
end
and i have this test:
clc, close all, clear all
%% SIGNAL CONSTRUCTION
% Signal duration
duration = 1;
% First frequency component
f1 = 10;
% Second frequency component
f2 = 20;
% Sampling frequency
Fs = 10*f2;
% Sampling period
Ts = 1/Fs;
% Vector of times
t = 0:Ts:duration;
% Signal construction
% Original signal without (10) + without (20)
xn = sin(2*pi*f1*t) + sin(2*pi*f2*t);
% N1 = length (xn);% The size of the sequence Xn is calculated
% FREQUENCY DOMAIN
% Number of signal samples
N = length (xn);
% Calculation of the value that is the closest multiple of 2 and multiplied by a
% factor to increase the number of samples and have a better result
nFFT = 2 ^ (ceil(log2(N)));
% Xn2 = my_fft_vectorial (xn);
% I apply the matlab FFT function
Xn1 = my_fft_for (xn);
% The module of the complexes called PERIODOGRAM is calculated
Periodogram = abs (Xn1);
% Calculation of the AXIS of frequencies: same number of points as PERIODOGRAM and that are between 0 and Fs:
f = linspace (0, Fs, nFFT);
% With the two axes ready, we just graph:
plot (f, Periodogram);
% Due to mid-period symmetry, we are only interested in visualizing half of the PERIDODOGRAM:
axis ([0 Fs/2 0 max(Periodogram)])
xlabel ('F (Hz)')
title ('PERIODOGRAM based on Matlab FFT')
Note:
i need transform from loop form to vector form is similar to this:
DFT loop form
DFT transform to vectorial form
HELP PLEASE!

採用された回答

Salman Ahmed
Salman Ahmed 2021 年 8 月 30 日
Hi Stalin,
I assume you need to vectorize all the loops in your code. You could refer to a possible workaround here that reduces two of the loops in your code. It is difficult to reduce the third loop without re-writing the logic as index and pos variables change size iteration to iteration. Hope it helps.
function [ y ] = my_fft_for( x )
N1 = length(x); % calculates the size of the Xn sequence
nFFT = 2^ceil(log2(N1)); % calculates the number of samples to complete
x =[x zeros(1,nFFT-N1)]; % The sequence is completed with 0's to nFFT elements
N=length(x); % calculates the number of samples of xn
b=bin2dec(fliplr(dec2bin (0:1:nFFT-1)))+1; % Reordering of samples (reverse bit)
x=x(b); % Signal reordered
S=log2(N); % calculates the number of stages
Half=1;
for stage=1:S % FOR loop of stages of the algorithm Log2 (N)
index=0:(2^stage):(N-1); % Butterflies for each stage of the algorithm
n=0:(Half-1); % A butterfly is calculated and the result is saved
pos=n+index'+1; % Index of samples
pow=(2^(S-stage)).*n; % Power of complex multiplication
w=exp((-1i)*(2*pi).*pow/N); % Complex multiplication
a=x(pos)+x(pos+Half).*w; % First part of the butterfly
b=x(pos)-x(pos+Half).*w; % Second part of the butterfly
x(pos)=a; % Saving result of the first part of the butterfly
x(pos+Half)=b; % Saving result of the second part of the butterfly
Half=2*Half;
end % Calculating the next "Half" value
y=x;
% The result of the function is saved
end
  1 件のコメント
Stalin García Ruano
Stalin García Ruano 2021 年 8 月 31 日
thanks man, the code works perfect!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by