Need help building circuit for 5V-12V Voltage regulation of external 12V power-source via PWM-GPIO of Raspberry Pi
What you ask is easy to do:



Something needs to produce the PWM pulses in the 25 to 50 kHz range.  I don't know if a RPi has PWM built in.  If not, have it drive a external PWM chip.  This is NOT something suitable to do in software, especially on a general purpose machine running a operating system, like the RPi.

Q1 is switched on and off according to the digital logic PWM signal.  D1 is NOT optional.  It protects the transistor from large inductive kickback voltages when it switches off.  It also allows the existing current to continue circulating during the off phase.  That reduces ripple, which minimizes the AC current that doesn't go to driving the motor but heats the windings.

There should be no need for any explicit "smoothing" of the voltage.  You haven't given any reason why you want to do that anyway.  The inductance of the motor windings will smooth the current themselves.