% Calculate two real FFTs by using only one complex FFT
clear;
N = 100; % number of samples
n = 0:N-1;
% Generate two real input signals (noisy offset sinewaves)
a = sin(5.3 * 2*pi * n/N - 0.9) + (rand(1,N)-0.5) + 0.2;
b = sin(7.3 * 2*pi * n/N + 0.8) + (rand(1,N)-0.5) - 0.3;
subplot(2,2,1); plot(n,real(a),n,imag(a)); title('Input signal a');
subplot(2,2,2); plot(n,real(b),n,imag(b)); title('Input signal b');
% Combine the two real signals, compute combined complex FFT
ab = a + j*b;
yab = fft(ab);
% Decompose the FFT result into the spectra of signals a and b
even = (yab + fliplr(circshift(yab,[0 -1]))) / 2; % even-odd decomposition
odd = (yab - fliplr(circshift(yab,[0 -1]))) / 2;
YA = real(even) + j*imag(odd);
YB = imag(even) - j*real(odd);
% Show that the spectra of YA and YB match the original input signals
A = ifft(YA);
B = ifft(YB);
subplot(2,2,3); plot(n,real(A),n,imag(A)); title('Output A');
subplot(2,2,4); plot(n,real(B),n,imag(B)); title('Output B');