function [X, delta]  = Power_spectrum(frame)
%   Step 1: Calculation of the power spectrum.
%   1. Apply hanning window h(n);
%   2. Calculate the power spectrum by FFT.
%   3. Normalize to 96dB.
%   Modified by Tina Lin

Common;

X = [];

N = length(frame);
% Pad zeros if samples are insufficient.
if N ~= FFT_SIZE  
    frame = [frame; zeros(1,FFT_SIZE-N)];
end

% Hanning window
h = sqrt(8/3) * hanning(512, 'periodic');
 
% Power density spectrum
if sum(frame) == 0
    frame = [1e-6, zeros(1,FFT_SIZE-1)];
end
X = max(20 * log10(abs(fft(frame .* h'))/ FFT_SIZE), MIN_POWER);

% % Figure 16 (a): Original power spectrum and normalized power spectrum
% figure(1);
% % Title('(a) Original power spectrum and normalized power spectrum');
% % Draw the original power spectrum 
% plot( X_LABEL, X(1:FFT_SIZE/2),'k:');
% hold on;

% Normalization
delta = 96 - max(X);
X = X + delta;

% % Figure 16 (a): Original power spectrum and normalized power spectrum
% % Draw the nomalized power spectrum 
% plot( X_LABEL, X(1:FFT_SIZE/2),'k'); 
% legend('original power spectrum','normalized power spectrum'); 
% hold off;
% xlabel('Frequency (Hz)'); ylabel('dB');
% axis([ 0 22050 -150 100]);