function get_statistics(frames, encoded_stream)

for n = 1 : length(frames)
    frame_in = frames(n).cdata;
    E_in(n) =  sum(sum(sum((double(frame_in)).^2 ))) ;

    frame_out = encoded_stream(n);
    E_luma(n) =  sum(sum( (frame_out.frame_data.encoded_luma).^2 )) ;
    E_chroma(n) =  sum(sum(sum((frame_out.frame_data.encoded_chroma).^2))) ;
    E_frame_out(n) = E_luma(n) + E_chroma(n);
    if isempty(frame_out.motion_vector_x)
        E_mv(n) = 0;
    else
        E_mv(n) =  sum( sum((frame_out.motion_vector_x).^2) ) +  sum( sum((frame_out.motion_vector_y).^2) );
    end
    E_total_out(n) = E_frame_out(n) + E_mv(n);
end
G = -20*log10( mean(sqrt(E_total_out))/mean(sqrt(E_in))  );
fprintf('Compression gain = %.2f dB \n', G );

figure
plot(20*log10(sqrt(E_in)))
hold on
plot(20*log10(sqrt(E_total_out)))
grid on
xlabel('frame []')
ylabel('20 log_{10} E [dB]')

end

