How do I find the transfer function of a speaker by analysing the audio produced by it?
Passive speaker or active speaker? If it's passive, then it's easy. If it's active, then you need to pull the transducer out and treat it as a passive speaker.
If you have a signal generator, use it as a source to generate white noise, otherwise use Matlab script.
If you are looking for impedance response, put your speaker in a voltage divider circuit (in series with a known resistor). The impedance of the speaker can be found by FFT the received signal (magnitude response and phase response are both useful). In this case you do need two devices / a tricky audio setup that generates and records at the same time.
If you are looking for surface pressure response, face your speaker at your mic (as close to each other as possible) and then play the same signal in an anechoic environment (no reverberation, ideally somewhere outdoors on a quiet sunny day) and do the same processing to received mic signal. Convert received voltages into pressure with your microphone's sensitivity data.
Impedance response tells you the physical property of the transducer and pressure response is what actually matters.