Hi,
There is a lot of information missing.
A picture could help.
Your question is: "is it possible.."
My anser is: YES.
using a 40vdc and a 5v squarewave ?
I assume you mean a sinlge 40V DC power supply (0V, +40V)
--> use a DPDT relay with 5V coil voltage:
* one NC1 to 40V, the NO1 from the same contact to 0V
* other contact NO2 to 40V, NC2 to 0V
* coil connected to GND and 5V square wave.
* output: COM1 and COM2
Connect a scope or DVM to COM1 abd COM2 and see +/-40 square wave.
****
Similar solutions can be made using an DPDT analog switch (faster, low power)
Or with a MOSFET power full bridge (fast, high power)
...and maybe hundreds of other solutions....depending on your (missing) specifications.
Klaus