% Frequency-domain windowing
%
% This worksheet will demonstrate frequency-domain
% windowing. It will first calculate the FFT for
% both the signal and the window function, then convolve
% these two functions before displaying the result.

clear all;
close all;

fs=1e6; % sample rate
N=2^13; % Number of points in the time record
fc=fs/N*1000.5; % Center frequency of sinewave
n=0:(N-1); % counter used for processing
f=(n-(N/2))*fs/N/1e6; % Frequency scale
%w=0.5-0.5*cos(2*pi*n/N); % Create Hann window
w=0.35875-0.48829*cos(2*pi*n/N)+0.14128*cos(4*pi*n/N)-0.01168*cos(6*pi*n/N); % BH92 window
s=exp(2j*pi*n*fc/fs); % Create complex sinusoid at half-bin boundary
sw=s.*w; % Windowed time domain

sw_freq=fftshift(20*log10(abs(fft(sw/N)))); % Time domain window spectrum

w_freq=[-0.00584,0.07064,-0.244145,0.35875,-0.244145,0.07064,-0.00584]; % BH92
s_freq=fft(s/N); % Create frequency domain of signal

% Calculate window in frequency domain by convolving signal and
% window frequency domains
windowed_freq=fftshift(20*log10(abs(conv2(s_freq,w_freq,'same'))));

% Create plots for two displays

% Frequency domain window plot
figure(1)
plot(f,windowed_freq)
xlim([-0.5,0.5]);
ylim([-200,0]);
title('Frequency Domain Window','fontsize',18);

% Time domain window plot
figure(2)
plot(f,sw_freq)
xlim([-0.5,0.5]);
ylim([-200,0]);
title('Time Domain Window','fontsize',18);
