A Bayesian method for inferring an optimal basis is applied to the problem of finding efficient codes for natural images. The key to the algorithm is multivariate non- Gaussian density estimation. This is equivalent, in various forms, to sparse coding or independent component analysis. The basis functions learned by the algorithm are oriented and localized in both space and frequency, bearing a resemblance to the spatial receptive fields of neurons in the primary visual cortex and to Gabor wavelet functions. An important advantage of the probabilistics framework is that it provides a method for comparing the coding efficiency of different bases objectively. The learned bases are shown to have better coding efficiency compared to traditional Fourier and wavelet bases. This framework can also be used to learn efficient codes of natural sound and the learned codes share many of the coding properties of the cochlear nerve. Time-frequency analysis is used to show that it is possible to derive both Fourier-like and wavelet-like representations by learning efficient codes for different classes of natural sounds.