ifft works on the full spectrum as pointed by Hilmar.
In the discrete description of Fourier Transform theory, it means that each sample supplied as the argument to
ifft is a frequency bin in the range $[0,1]$ in cycles per sample (see normalized frequency if you do not know what that means), which corresponds to $[0,2\pi]$ radians per sample or $[0, F_s]$ sample per second.
ifft saw is a spectrum ranging from (assuming $F_s=1000 Hz$, which as you see below is completely arbitrary):
- 0 to 1000 Hz in the first case, with a Gaussian in magnitude spectrum around 500 Hz
- 0 to 40 Hz in the second case, with a Gaussian in magnitude spectrum around 20 Hz
Although it gets a bit more tricky, as your Gaussian is peaking right at the Nyquist frequency, you are simply retrieving an oscillation in time-domain at the limit of your sampling rate. In both case your spectrum is the same shape but only of different length, if you assume the signal is of fixed duration that means you are transforming two signals with different sampling frequency.
It would be more appropriate to define first the frequency range as
sampleRate = 1000;
duration = 1; % 1 second
N = duration * sampleRate;
delta_f = N/sampleRate; % frequency resolution: this is fixed by all other parameters
freqs = 0:delta_f:sampleRate
% Then create your gaussian spectrum around whatever value (but below Nyquist!)
mu = 250;
sigma = 50;
intensityFreq = exp(-(freqs-mu).^2/sigma);
% etc... Play around with mu, center frequency of your gaussian, and sigma.
All in all, I would advise you to read a bit more about discrete Fourier transform and inverse Fourier transform.
Also, from your example code you will get a complex valued time-domain signal. If you were aiming for a real-valued signal, of course the magnitude spectrum supplied must be symmetric around the Nyquist frequency $Fs/2$ (and more precisely the complex valued spectrum must be complex conjugate). But this is another matter, and you should read more about Fourier transform theory if this is unknown to you (this answer is a good read on the matter).
% To transform the complex spectrum such that the second half is the complex conjugate of the first (avoiding DC 0th bin) do:
signalFreq = sqrt(intensityFreq)*exp(1i*freqs); % assuming intensityFreq is defined on one half only, 0 on range [fs/2:fs]
signalFreq(end-1:-1:end/2+1) = conj(signalFreq(2:end/2));
% You can use the option 'symmetric' to assume that the argument is conjugate symmetric:
signalTime = ifft(signalFreq, 'symmetric');