%% Create Vector-Input FFT for HDL Generation
%%   
% Create specifications and input signal. This example uses a
% 128-point FFT and computes the transform over 16 samples at a time.
N = 128;
V = 16;
Fs = 40; 
t = (0:N-1)'/Fs;
x = sin(2*pi*15*t) + 0.75*cos(2*pi*10*t);
y = x + .25*randn(size(x));
y_fixed = sfi(y,32,24);
y_vect = reshape(y_fixed,V,N/V);
%% 
% Write a function that creates and calls the System object(TM). The 
% function does not need to know the vector size. The 
% object saves the size of the input signal the first time you call it. 
% 
% *Note:* This object syntax runs only in R2016b or later. If you are using an 
% earlier release, replace each call of an object with the equivalent |step|
% syntax. For example, replace |myObject(x)| with |step(myObject,x)|.
%
% <include>HDLFFT128V16.m</include> 
%
%% 
% Compute the FFT by passing 16-element vectors to the object. Use the
% |getLatency| function to find out when the first output data sample will be 
% ready. Then, add the frame length to determine how many times to call the
% object. Because the object variable is inside the function, use a second
% object to call |getLatency|. Use the loop counter to flip |validIn| 
% to |false| after _N_ input samples.
tempfft = dsp.HDLFFT;
loopCount = getLatency(tempfft,N,V)+N/V;
Yf = zeros(V,loopCount);
validOut = false(V,loopCount);
for loop = 1:1:loopCount 
    if ( mod(loop,N/V) == 0 ) 
        i = N/V; 
    else
        i = mod(loop,N/V); 
    end
    [Yf(:,loop),validOut(loop)] = HDLFFT128V16(complex(y_vect(:,i)),(loop<=N/V)); 
end
%% 
% Discard invalid output samples.
C = Yf(:,validOut==1);
Yf_flat = C(:);
%% 
% Plot the frequency channel data from the FFT. The FFT output is in
% bit-reversed order. Reorder it before plotting.
Yr =  bitrevorder(Yf_flat);
plot(Fs/2*linspace(0,1,N/2),2*abs(Yr(1:N/2)/N))
title('Single-Sided Amplitude Spectrum of Noisy Signal y(t)')
xlabel('Frequency (Hz)')
ylabel('Output of FFT (f)')

%% 
% Copyright 2012 The MathWorks, Inc.