import random


def freq_mask(spec, F=10, num_masks=2, replace_with_zero=False):
    """Frequency Mask is intuitively described as mask certain frequency bands with 
        either the mean value of the spectrogram or zero.
    
        Parameter
        ---------
            spec : 2-dim array with shape [F, T]
                2-dim feature, such spectrogram, mel-fbank, or MFCC
            F : int
                maximum number of frequency mask
            num_masks : int
                number of mask
            replace_with_zeros : bool, False
                to determine either mean or zero
            
        Return
        ------
            masked spec : 2-dim array with shape [F, T]
    """
    cloned = spec.copy()
    num_mel_channels = cloned.shape[0]
    for i in range(0, num_masks):        
        f = random.randrange(0, F)
        f_zero = random.randrange(0, num_mel_channels - f)
        if (f_zero == f_zero + f): 
            return cloned
        mask_end = random.randrange(f_zero, f_zero + f) + 1
        if replace_with_zero is True: 
            cloned[f_zero:mask_end] = 0
        else: 
            cloned[f_zero:mask_end] = cloned.mean()
    return cloned