function  magnitude =Psychoacoustic_model_I(x,fs) 
% Calculate inaudible amount in frequency domain as amplitude of the watermark signal, 
% implemented by Psychoacoustic Model I.
% x: Input frame, with 512 points 
% fs: Sampling frequency (44100Hz) 
% Reference: 
% [21]	ISO/IEC 11172-3, Information Technology - Coding of Moving Picture and Associated Audio 
%       for Digital Storage Media Up To About 1.5Mbit/s, British Standard. BSI, London, 1993.
% [22]	F.A.P. Petitcolas, MPEG for Matlab, 2003.
%       www.petitcolas.net/fabien/software/mpeg

global delta;
Common;

% Load tables.
[TH, Map, LTq] = Table_absolute_threshold(1, fs, 128); % Threshold in quiet
CB = Table_critical_band_boundaries(1, fs);

% Step 1: Calculation of the power spectrum 
[X, delta] = Power_Spectrum(x);
  
% Step 2: Identification of tonal and non-tonal maskers
[Flags, Tonal_list, Non_tonal_list] = Find_tonal_and_nontonal_components(X, TH, Map, CB);
   
% Step 3: Decimation of the invalid tonal and non-tonal maskers
[Flags, Tonal_list, Non_tonal_list] = Decimation(X, Tonal_list, Non_tonal_list, Flags, TH, Map);
   
% Step 4: Calculation of individual masking thresholds
[LTt, LTn] = Individual_masking_thresholds(X, Tonal_list, Non_tonal_list, TH, Map);
   
% Step 5: Calculation of global masking threshold
LTg = Global_masking_threshold(LTq, LTt, LTn);
 
% Step 6: Determination of the minimum masking threshold
LTmin = Minimum_masking_threshold(LTg, Map);

% Step 7: Determination of magnitude of watermark signal
% Different methods, still under test.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Please choose!!! %%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
choice = 1;
% 1. The magnitude of watermark signal is piecewise linear MMT.
% 2. The magnitude of watermark signal is smooth MMT.
% 3. The magnitude of watermark signal is the winowed magnitude 
%    of host signal after shaped by piecewise linear MMT.
% 4. The magnitude of watermark signal is the winowed magnitude 
%    of host signal after shaped by smooth linear MMT.
% 5. The magnitude of watermark signal is the magnitude 
%    of host signal after shaped by piecewise linear MMT.
% 6. The magnitude of watermark signal is the magnitude 
%    of host signal after shaped by smooth linear MMT.
% 7. The magnitude response of the windowed host frame acts as 
%    the magnitude of the watermark signal. 
% 8. The magnitude response of the host frame acts as 
%    the magnitude of the watermark signal.
if choice == 1
    % Minimum masking threshold acts as the magnitude of watermark signal directly.
    % (1) Piecewise linear lines by spreading over each subband
    count = 1;
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    for n = 1 : N_SUBBAND          % For each subband
        for j = 1 : Subband_size   % Spread over the whole subband
            LTminfreq_dB( count ) = LTmin(n);
            count = count + 1;
        end
    end
    factor = 100;
    for i = 1: FFT_SIZE/2
        magnitude(i) = 10^( ( LTminfreq_dB(i) - delta)/20 ) * factor; 
    end
elseif choice == 2
    % (2) Smooth curve by interpolation
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    old_index = (Subband_size/2+1) : Subband_size : (FFT_SIZE/2-Subband_size/2+1); % LTmin has 32 outputs
    new_index = 1 : FFT_SIZE/2;
    LTminfreq_dB = interp1(old_index,LTmin,new_index,'spline');
    factor = 100;
    for i = 1: FFT_SIZE/2
        magnitude(i) = 10^( ( LTminfreq_dB(i) - delta)/20 ) * factor; 
    end
    
    % The magnitude of the watermark signal is the magnitude response of
    % the host frame after shaped by the minimum masking threshold.
elseif choice == 3
    % (3) Piecewise linear & Windowed
    count = 1;
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    for n = 1 : N_SUBBAND          % For each subband
        for j = 1 : Subband_size   % Spread over the whole subband
            LTminfreq_dB( count ) = LTmin(n);
            count = count + 1;
        end
    end
    h = hanning(512)'; 
    Fx = abs(fft( x .* h ));       % magnitude response
    factor = 100;
    for i = 1: FFT_SIZE/2   
        magnitude(i) = factor * Fx(i) * 10^( ( LTminfreq_dB(i) - delta)/20 );     
    end
elseif choice == 4 
    % (4) Smooth & Windowed
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    old_index = (Subband_size/2+1) : Subband_size : (FFT_SIZE/2-Subband_size/2+1); 
    new_index = 1 : FFT_SIZE/2;
    LTminfreq_dB = interp1(old_index,LTmin,new_index,'spline');
    h = hanning(512)'; 
    Fx = abs(fft( x .* h ));       % magnitude response
    factor = 100;
    for i = 1: FFT_SIZE/2   
        magnitude(i) = factor * Fx(i) * 10^( ( LTminfreq_dB(i) - delta)/20 );     
    end
elseif choice == 5
    % (5) Piecewise & Not windowed
    count = 1;
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    for n = 1 : N_SUBBAND          % For each subband
        for j = 1 : Subband_size   % Spread over the whole subband
            LTminfreq_dB( count ) = LTmin(n);
            count = count + 1;
        end
    end 
    Fx = abs(fft( x ));            % magnitude response
%     factor = 100;
%     for i = 1: FFT_SIZE/2   
%         magnitude(i) = factor * Fx(i) * 10^( ( LTminfreq_dB(i) - delta)/20 );     
%     end
    temp = Fx(1 : FFT_SIZE/2).*LTminfreq_dB;   % not transfer dB to decimal, directly scale
    magnitude = temp/10000;
elseif choice == 6
    % (6) Smooth & Not windowed
    Subband_size = FFT_SIZE / 2 / N_SUBBAND;
    old_index = (Subband_size/2+1) : Subband_size : (FFT_SIZE/2-Subband_size/2+1); 
    new_index = 1 : FFT_SIZE/2;
    LTminfreq_dB = interp1(old_index,LTmin,new_index,'spline');
    Fx = abs(fft( x ));           % magnitude response
    factor = 100;
    for i = 1: FFT_SIZE/2   
        magnitude(i) = factor * Fx(i) * 10^( ( LTminfreq_dB(i) - delta)/20 );     
    end
%     for i = 1: FFT_SIZE/2   
%         magnitude1(i) = factor * Fx(i) * 10^( ( LTminfreq_dB(i) - delta)/20 );     
%     end
%     temp = LTminfreq_dB/max(LTminfreq_dB);      % not transfer dB to decimal, normalise MMT 
%     magnitude2 = 0.1 * Fx(1:FFT_SIZE/2).*temp;
%     if max(magnitude2) > max(magnitude1)
%         ff = max(magnitude2)/max(magnitude1);
%     else
%         ff = 1;
%     end
%     magnitude = magnitude2/ff;
    
    % Magnitude response of the host frame acts as magnitude of the watermark signal. 
elseif choice == 7
    % (7) Windowed
    h = hanning(512)'; 
    Fx = abs(fft( x .* h ));       % magnitude response
    factor = 0.1;
    magnitude = factor * Fx(1 : FFT_SIZE/2);
else
    Fx = abs(fft( x ));            % magnitude response
    factor = 0.1;
    magnitude = factor * Fx(1 : FFT_SIZE/2);
end
% The results of 1 and 2 are similar on the aspect of SSNR, around 33dB.  
% Right for detection, but the magnitude of the watermark signal is low, around 0.02.
% The results of 3 and 4 are better than those of 5 and 6 on the aspect of SSNR, more than 40dB.
% Wrong for detection, but the magnitude of the watermark signal is rather high, around 0.1.
% The results of 7 and 8 are similar on the aspect of SSNR, around 
return;
