We perform numerical simulations of magnetoacoustic waves (MAWs) in the solar atmosphere, which is gravitationally stratified and structured by either vertical or horizontal uniform magnetic fields. These waves are excited by a monochromatic driver that operates in the photosphere. We show that the gradients of the atmospheric parameters lead to filtering of the waves through the solar atmosphere and to variations of the dominant wave period with height. We use these variations to determine a local cutoff period, which shows a good agreement with the previously obtained analytical and numerical results in an isothermal solar atmosphere. In our numerical simulations, the propagation of MAWs in a more realistic model of the solar atmosphere is considered, and the obtained results demonstrate that the waves with periods higher than a local cutoff wave period are strongly reflected and become evanescent with height, while the waves with shorter wave periods are propagating, and may even reach the solar corona. Some of the evanescent waves may also tunnel and reach the atmospheric heights that would not be otherwise accessible to them. An important result of our study is excitation of chromospheric oscillations with periods equal to the period that is comparable to the observed solar chromospheric oscillations. Implications of our theoretical predictions are discussed.