Set Phasors to Shift -or- A Better Phase Shifter






Thanks to the Do The Math! article on Phase and Mixers, it suddenly occurs to me a better method of realizing phase shifts in gnuradio: by complex mixing. The method used here, with a fir filter delaying by a number of steps, has some problems, namely being dependant on signal frequency and sample rate. A much better method is to simply multiply a complex signal by a unit phasor at some angle. The unit phasor can itself be advanced or retarted by multiplying by a small phasor, say 1/100th of a circle (360 degrees or 2π, divided by 100).

Here's the improved method:
        src0a = gr.sig_source_c (sample_freq, gr.GR_SIN_WAVE, 1e4, ampl)
        self.src1a = gr.multiply_const_cc(complex(-1,0))
        self.fg.connect (src0a, self.src1a)
        src0 = gr.complex_to_float()
        self.src1 = gr.complex_to_float()
        self.fg.connect(src0a,src0)
        self.fg.connect(self.src1a,self.src1)

        self.rot_ccw = complex(math.cos(math.pi/50),math.sin(math.pi/50))
        self.rot_cw = complex(math.cos(math.pi/50),math.sin(-math.pi/50))
The source is renamed to src0a and made complex. The phase shifter is renamed self.src1a and made a complex multiplier, and initialized with -π, and connected to emit a phase shifted signal from src0a, then converted to float with the old source names to fit the rest of the graph. Now, to change the phase we only have to change the constant in self.src1a, say by multiplying that constant by self.rot_ccw or self.rot_cw. For example, when a control knob is turned:
    def on_rotate(self, evt):
        if evt.delta > 0:
          self.src1a.set_k(self.src1a.k()*self.rot_ccw)
        else:
          self.src1a.set_k(self.src1a.k()*self.rot_cw)
If the knob is turned one way, the phase shift constant is advanced by self.rot_ccw, and the other way by self.rot_cw.

Updated lab