Ejemplo n.º 1
0
        /**
         * Used for debugging purposes.
         */
        public static void Print(TextWriter @out, TriangleFilter f)
        {
            for (int i = 0; i < f.left_edge_; ++i)
            {
                if (i != 0)
                {
                    @out.Write(", ");
                }
                @out.Write("0");
            }

            for (int i = 0; i < f.size_; ++i)
            {
                //@out.Write(", " + f.filter_data_[i].ToString("0.000", CultureInfo.InvariantCulture));
                @out.Write(", " + f.filter_data_[i].ToString(numberFormat));
            }
        }
Ejemplo n.º 2
0
		public MelFilterBank(float min_freq, float max_freq, int num_mel_bands, int num_bins, int sample_rate, bool normalize_filter_area)
		{
			min_freq_ = min_freq;
			max_freq_ = max_freq;
			num_mel_bands_ = num_mel_bands;
			num_bins_ = num_bins;
			sample_rate_ = sample_rate;
			normalize_filter_area_ = normalize_filter_area;
			
			//Let's do some argument checking
			if ((min_freq >= max_freq) || (max_freq == 0))
			{
				throw new Exception(String.Format("Invalid min/max frequencies for MelFilterBank: min = '{0}' max = '{1}'", min_freq, max_freq));
			}
			
			if (num_mel_bands == 0)
			{
				throw new Exception(String.Format("Invalid number of mel bands for MelFilterBank: n = {0}", num_mel_bands));
			}
			
			if (sample_rate == 0)
			{
				throw new Exception(String.Format("Invalid sample rate for MelFilterBank: s = {0}", sample_rate));
			}
			
			if (num_bins == 0)
			{
				throw new Exception(String.Format("Invalid number of bins for MelFilterBank: s = '{0}'", num_bins));
			}
			
			float delta_freq = (float)sample_rate_ / (2 *num_bins);
			
			float mel_min = (float) HzToMel(min_freq_);
			float mel_max = (float) HzToMel(max_freq_);
			
			// We divide by #band + 1 as min / max should present the beginning / end
			// of beginng up / ending low slope, i.e. it's not the centers of each
			// band that represent min/max frequency in mel bands.
			float delta_freq_mel = (mel_max - mel_min) / (num_mel_bands_ + 1);
			
			// Fill up equidistant spacing in mel-space
			float mel_left = mel_min;
			for (int i = 0; i < num_mel_bands_; i++)
			{
				float mel_center = mel_left + delta_freq_mel;
				float mel_right = mel_center + delta_freq_mel;
				
				float left_hz = (float) MelToHz(mel_left);
				float right_hz = (float) MelToHz(mel_right);
				
				//align to closest num_bins (round)
				int left_bin = (int)((left_hz / delta_freq) + 0.5f);
				int right_bin = (int)((right_hz / delta_freq) + 0.5f);
				
				//calculate normalized height
				float height = 1.0f;
				
				if (normalize_filter_area_)
					height = 2.0f / (right_bin - left_bin);
				
				// Create the actual filter
				TriangleFilter fltr = new TriangleFilter(left_bin, right_bin, height);
				filters_.Add(fltr);
				
				//next left edge is current center
				mel_left = mel_center;
			}
		}
Ejemplo n.º 3
0
        public MelFilterBank(float min_freq, float max_freq, int num_mel_bands, int num_bins, int sample_rate, bool normalize_filter_area)
        {
            min_freq_              = min_freq;
            max_freq_              = max_freq;
            num_mel_bands_         = num_mel_bands;
            num_bins_              = num_bins;
            sample_rate_           = sample_rate;
            normalize_filter_area_ = normalize_filter_area;

            //Let's do some argument checking
            if ((min_freq >= max_freq) || (max_freq == 0))
            {
                throw new Exception(String.Format("Invalid min/max frequencies for MelFilterBank: min = '{0}' max = '{1}'", min_freq, max_freq));
            }

            if (num_mel_bands == 0)
            {
                throw new Exception(String.Format("Invalid number of mel bands for MelFilterBank: n = {0}", num_mel_bands));
            }

            if (sample_rate == 0)
            {
                throw new Exception(String.Format("Invalid sample rate for MelFilterBank: s = {0}", sample_rate));
            }

            if (num_bins == 0)
            {
                throw new Exception(String.Format("Invalid number of bins for MelFilterBank: s = '{0}'", num_bins));
            }

            float delta_freq = (float)sample_rate_ / (2 * num_bins);

            float mel_min = (float)HzToMel(min_freq_);
            float mel_max = (float)HzToMel(max_freq_);

            // We divide by #band + 1 as min / max should present the beginning / end
            // of beginng up / ending low slope, i.e. it's not the centers of each
            // band that represent min/max frequency in mel bands.
            float delta_freq_mel = (mel_max - mel_min) / (num_mel_bands_ + 1);

            // Fill up equidistant spacing in mel-space
            float mel_left = mel_min;

            for (int i = 0; i < num_mel_bands_; i++)
            {
                float mel_center = mel_left + delta_freq_mel;
                float mel_right  = mel_center + delta_freq_mel;

                float left_hz  = (float)MelToHz(mel_left);
                float right_hz = (float)MelToHz(mel_right);

                //align to closest num_bins (round)
                int left_bin  = (int)((left_hz / delta_freq) + 0.5f);
                int right_bin = (int)((right_hz / delta_freq) + 0.5f);

                //calculate normalized height
                float height = 1.0f;

                if (normalize_filter_area_)
                {
                    height = 2.0f / (right_bin - left_bin);
                }

                // Create the actual filter
                TriangleFilter fltr = new TriangleFilter(left_bin, right_bin, height);
                filters_.Add(fltr);

                //next left edge is current center
                mel_left = mel_center;
            }
        }
Ejemplo n.º 4
0
		/**
		 * Used for debugging purposes.
		 */
		public static void Print(TextWriter @out, TriangleFilter f)
		{
			for (int i = 0; i < f.left_edge_; ++i)
			{
				if (i != 0)
					@out.Write(", ");
				@out.Write("0");
			}

			for (int i = 0; i < f.size_; ++i)
			{
				//@out.Write(", " + f.filter_data_[i].ToString("0.000", CultureInfo.InvariantCulture));
				@out.Write(", " + f.filter_data_[i].ToString(numberFormat));
			}
		}