%% Cross Correlate or Autocorrelate Input Data
% Estimate the cross correlation or autocorrelation of two real-valued input vectors to lag 50. Output the estimate at the nonnegative lags.

type myxcorr

%
% Run the codegen command:

codegen myxcorr -args {zeros(512,1), zeros(512,1)} -o myxcorr_mex -report

%
% Use the MEX-file to compute and plot the autocorrelation of a white noise input:

rng(0,'twister')

% Use a White noise input signal and compute its autocorrelation using both the MATLAB function as well as the MEX file. Plot the results.

input_data = randn(512,1);
[M,mexLags] = myxcorr_mex(input_data,input_data);

[C,Lags] = myxcorr(input_data,input_data);
stem(mexLags,M); 
axis([-0.5 51 -1.1 1.1])

%

hold on
stem(Lags,C,':*r'); 
axis([-0.5 51 -1.1 1.1])
xlabel('Lags'); 
ylabel('Autocorrelation Function'); 
legend('MEX Output','Matlab Output')

% Copyright 2018 The MathWorks, Inc.