In this paper a method for designing waveforms for temporal encoding in medical ultrasound imaging is described. The method is based on least squares optimization and is used to design nonlinear frequency modulated signals for synthetic transmit aperture imaging. By using the proposed design method, the amplitude spectrum of the transmitted waveform can be optimized, such that most of the energy is transmitted where the transducer has large amplification. To test the design method, a waveform was designed for a BK8804 linear array transducer. The resulting nonlinear frequency modulated waveform was compared to a linear frequency modulated signal with amplitude tapering, previously used in clinical studies for synthetic transmit aperture imaging. The latter had a relatively flat spectrum which implied that the waveform tried to excite all frequencies including ones with low amplification. The proposed waveform, on the other hand, was designed so that only frequencies where the transducer had a large amplification were excited. Hereby, unnecessary heating of the transducer could be avoided and the signal-to-noise ratio could be increased. The experimental ultrasound scanner RASMUS was used to evaluate the method experimentally. Due to the careful waveform design optimized for the transducer at hand, a theoretic gain in signal-to-noise ratio of 4.9 dB compared to the reference excitation was found, even though the energy of the nonlinear frequency modulated signal was 71% of the energy of the reference signal. This was supported by a signal-to-noise ratio measurement and comparison in penetration depth, where an increase of 1 cm was found in favor for the proposed waveform. Axial and lateral resolutions at full-width half-maximum were compared in a water phantom at depths of 42, 62, 82, and 102 mm. The axial resolutions of the nonlinear frequency modulated signal were 0.62, 0.69, 0.60, and 0.60 mm, respectively. The corresponding axial resolutions for the reference waveform were 0.58, 0.65, 0.62, and 0.60 mm, respectively. The compression properties of the matched filter (mismatched filter for the linear frequency modulated signal) were tested for both waveforms in simulation with respect to the Doppler frequency shift occurring when probing moving objects. It was concluded that the Doppler effect of moving targets does not significantly degrade the filtered output. Finally, in vivo measurements are shown for both methods, wherein the common carotid artery on a 27-year-old healthy male was scanned.