static void Sound_into_BarkSpectrogram_frame (Sound me, BarkSpectrogram thee, long frame) {
	autoSpectrum him = Sound_to_Spectrum_power (me);
	long numberOfFrequencies = his nx;
	autoNUMvector<double> z (1, numberOfFrequencies);

	for (long ifreq = 1; ifreq <= numberOfFrequencies; ifreq++) {
		double fhz = his x1 + (ifreq - 1) * his dx;
		z[ifreq] = thy v_hertzToFrequency (fhz);
	}

	for (long i = 1; i <= thy ny; i++) {
		double p = 0;
		double z0 = thy y1 + (i - 1) * thy dy;
		double *pow = his z[1]; // TODO ??
		for (long ifreq = 1; ifreq <= numberOfFrequencies; ifreq++) {
			// Sekey & Hanson filter is defined in the power domain.
			// We therefore multiply the power with a (and not a^2).
			// integral (F(z),z=0..25) = 1.58/9

			double a = NUMsekeyhansonfilter_amplitude (z0, z[ifreq]);
			p += a * pow[ifreq] ;
		}
		thy z[i][frame] = p;
	}
}
// xmin, xmax in hz versus bark/mel or lin
void BandFilterSpectrogram_drawFrequencyScale (BandFilterSpectrogram me, Graphics g, double xmin, double xmax, double ymin, double ymax, int garnish) {
	if (xmin < 0 || xmax < 0 || ymin < 0 || ymax < 0) {
		Melder_warning (U"Frequencies must be >= 0.");
		return;
	}

	// scale is in hertz
	if (xmin >= xmax) { // autoscaling
		xmin = 0;
		xmax = my v_frequencyToHertz (my ymax);
	}

	if (ymin >= ymax) { // autoscaling
		ymin = my ymin;
		ymax = my ymax;
	}

	long n = 2000;

	Graphics_setInner (g);
	Graphics_setWindow (g, xmin, xmax, ymin, ymax);

	double dx = (xmax - xmin) / (n - 1);
	double x1 = xmin, y1 = my v_hertzToFrequency (x1);
	for (long i = 2; i <= n;  i++) {
		double x2 = x1 + dx, y2 = my v_hertzToFrequency (x2);
		if (NUMdefined (y1) && NUMdefined (y2)) {
			double xo1, yo1, xo2, yo2;
			if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, &xo1, &yo1, &xo2, &yo2)) {
				Graphics_line (g, xo1, yo1, xo2, yo2);
			}
		}
		x1 = x2; y1 = y2;
	}
	Graphics_unsetInner (g);

	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_marksLeft (g, 2, 1, 1, 0);
		Graphics_textLeft (g, 1, Melder_cat (U"Frequency (", my v_getFrequencyUnit (), U")"));
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_textBottom (g, 1, U"Frequency (Hz)");
	}
}
void MelSpectrogram_drawTriangularFilterFunctions (MelSpectrogram me, Graphics g, bool xIsHertz, int fromFilter, int toFilter, double zmin, double zmax, bool yscale_dB, double ymin, double ymax, int garnish) {
	double xmin = zmin, xmax = zmax;
	if (zmin >= zmax) {
		zmin = my ymin; zmax = my ymax; // mel
		xmin = xIsHertz ? my v_frequencyToHertz (zmin) : zmin;
		xmax = xIsHertz ? my v_frequencyToHertz (zmax) : zmax;
	}
	if (xIsHertz) {
		zmin = my v_hertzToFrequency (xmin); zmax = my v_hertzToFrequency (xmax);
	}

	if (ymin >= ymax) {
		ymin = yscale_dB ? -60 : 0;
		ymax = yscale_dB ? 0 : 1;
	}
	fromFilter = fromFilter <= 0 ? 1 : fromFilter;
	toFilter = toFilter <= 0 || toFilter > my ny ? my ny : toFilter;
	if (fromFilter > toFilter) {
		fromFilter = 1; toFilter = my ny;
	}
	
	long n = xIsHertz ? 1000 : 500;
	autoNUMvector<double> xz (1, n), xhz (1,n), y (1, n);

	Graphics_setInner (g);
	Graphics_setWindow (g, xmin, xmax, ymin, ymax);
	
	double dz = (zmax - zmin) / (n - 1);
	for (long iz = 1; iz <= n; iz++) {
		double f = zmin + (iz - 1) * dz;
		xz[iz] = f;
		xhz[iz] = my v_frequencyToHertz (f); // just in case we need the linear scale
	}
	
	for (long ifilter = fromFilter; ifilter <= toFilter; ifilter++) {
		double zc = Matrix_rowToY (me, ifilter), zl = zc - my dy, zh = zc + my dy;
		double xo1, yo1, xo2, yo2;
		if (yscale_dB) {
			for (long iz = 1; iz <= n; iz++) {
				double z = xz[iz];
				double amp = NUMtriangularfilter_amplitude (zl, zc, zh, z);
				y[iz] = yscale_dB ? (amp > 0 ? 20 * log10 (amp) : ymin - 10) : amp;
			}
			double x1 = xIsHertz ? xhz[1] : xz[1], y1 = y[1];
			if (NUMdefined (y1)) {
				for (long iz = 1; iz <= n; iz++) {
					double x2 = xIsHertz ? xhz[iz] : xz[iz], y2 = y[iz];
					if (NUMdefined (y2)) {
						if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, &xo1, &yo1, &xo2, &yo2)) {
							Graphics_line (g, xo1, yo1, xo2, yo2);
						}
					}
					x1 = x2; y1 = y2;
				}
			}
		} else {
			double x1 = xIsHertz ? my v_frequencyToHertz (zl) : zl;
			double x2 = xIsHertz ? my v_frequencyToHertz (zc) : zc;
			if (NUMclipLineWithinRectangle (x1, 0, x2, 1, xmin, ymin, xmax, ymax, &xo1, &yo1, &xo2, &yo2)) {
				Graphics_line (g, xo1, yo1, xo2, yo2);
			}
			double x3 = xIsHertz ? my v_frequencyToHertz (zh) : zh;
			if (NUMclipLineWithinRectangle (x2, 1, x3, 0, xmin, ymin, xmax, ymax, &xo1, &yo1, &xo2, &yo2)) {
				Graphics_line (g, xo1, yo1, xo2, yo2);
			}
		}
	}

	Graphics_unsetInner (g);

	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_marksLeftEvery (g, 1, yscale_dB ? 10 : 0.5, 1, 1, 0);
		Graphics_textLeft (g, 1, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
		Graphics_textBottom (g, 1, Melder_cat (U"Frequency (", ( xIsHertz ? U"Hz" : my v_getFrequencyUnit () ), U")"));
	}
}
void BarkSpectrogram_drawSekeyHansonFilterFunctions (BarkSpectrogram me, Graphics g, bool xIsHertz, int fromFilter, int toFilter, double zmin, double zmax, bool yscale_dB, double ymin, double ymax, int garnish) {
	double xmin = zmin, xmax = zmax;
	if (zmin >= zmax) {
		zmin = my ymin; zmax = my ymax;
		xmin = xIsHertz ? my v_frequencyToHertz (zmin) : zmin;
		xmax = xIsHertz ? my v_frequencyToHertz (zmax) : zmax;
	}
	if (xIsHertz) {
		zmin = my v_hertzToFrequency (xmin); zmax = my v_hertzToFrequency (xmax);
	}
	if (ymin >= ymax) {
		ymin = yscale_dB ? -60 : 0;
		ymax = yscale_dB ? 0 : 1;
	}
	fromFilter = fromFilter <= 0 ? 1 : fromFilter;
	toFilter = toFilter <= 0 || toFilter > my ny ? my ny : toFilter;
	if (fromFilter > toFilter) {
		fromFilter = 1; toFilter = my ny;
	}
	long n = xIsHertz ? 1000 : 500;
	autoNUMvector<double> xz (1, n), xhz (1,n), y (1, n);

	Graphics_setInner (g);
	Graphics_setWindow (g, xmin, xmax, ymin, ymax);

	double dz = (zmax - zmin) / (n - 1);
	for (long iz = 1; iz <= n; iz++) {
		double f = zmin + (iz - 1) * dz;
		xz[iz] = f;
		xhz[iz] = my v_frequencyToHertz (f); // just in case we need the linear scale
	}
	for (long ifilter = fromFilter; ifilter <= toFilter; ifilter++) {
		double zMid = Matrix_rowToY (me, ifilter);
		for (long iz = 1; iz <= n; iz++) {
			double z = xz[iz] - (zMid - 0.215);
			double amp = 7 - 7.5 * z - 17.5 * sqrt (0.196 + z * z);
			y[iz] = yscale_dB ? amp : pow (10, amp / 10);
		}
		// the drawing
		double x1 = xIsHertz ? xhz[1] : xz[1], y1 = y[1];
		for (long iz = 2; iz <= n; iz++) {
			double x2 = xIsHertz ? xhz[iz] : xz[iz], y2 = y[iz];
			if (NUMdefined (x1) && NUMdefined (x2)) {
				double xo1, yo1, xo2, yo2;
				if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, &xo1, &yo1, &xo2, &yo2)) {
					Graphics_line (g, xo1, yo1, xo2, yo2);
				}
			}
			x1 = x2; y1 = y2;
		}
	}	
	Graphics_unsetInner (g);

	if (garnish) {
		double distance = yscale_dB ? 10 : 0.5;
		Graphics_drawInnerBox (g);
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_marksLeftEvery (g, 1, distance, 1, 1, 0);
		Graphics_textLeft (g, 1, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
		Graphics_textBottom (g, 1, Melder_cat (U"Frequency (", xIsHertz ? U"Hz" : my v_getFrequencyUnit (), U")"));
	}
}