Chemical recognition in terahertz time-domain spectroscopy and imaging