Generation of terahertz radiation in nonlinear waveguides