%% Multiple Coherence and Ordinary Coherence
% Generate a random two-channel signal, x. Generate another signal, y, by lowpass filtering the two channels and adding them together. Specify a 30th-order FIR filter with a cutoff frequency of 0.3π and designed using a rectangular window.

h = fir1(30,0.3,rectwin(31));
x = randn(16384,2);
y = sum(filter(h,1,x),2);

%
% Compute the multiple-coherence estimate of x and y. Window the signals with a 1024-sample Hann window. Specify 512 samples of overlap between adjoining segments and 1024 DFT points. Plot the estimate.

noverlap = 512;
nfft = 1024;

mscohere(x,y,hann(nfft),noverlap,nfft,'mimo')

%
% Compare the coherence estimate to the frequency response of the filter. The drops in coherence correspond to the zeros of the frequency response.

[H,f] = freqz(h);

hold on
yyaxis right
plot(f/pi,20*log10(abs(H)))
hold off

%
% Compute and plot the ordinary magnitude-squared coherence estimate of x and y. The estimate does not reach 1 for any of the channels.

figure
mscohere(x,y,hann(nfft),noverlap,nfft)

% Copyright 2012 The MathWorks, Inc.