title: Matlab to Python code conversion: Binary phase-shift keying (BPSK)
body: <p>I have this MATLAB code:</p>
  
  <pre><code>d=[1 0 1 1 0]; % Data sequence
  b=2*d-1; % Convert unipolar to bipolar
  T=1; % Bit duration
  Eb=T/2; % This will result in unit amplitude waveforms
  fc=3/T; % Carrier frequency
  t=linspace(0,5,1000); % discrete time sequence between 0 and 5*T (1000 samples)
  N=length(t); % Number of samples
  Nsb=N/length(d); % Number of samples per bit
  dd=repmat(d',1,Nsb); % replicate each bit Nsb times
  bb=repmat(b',1,Nsb); dw=dd'; % Transpose the rows and columns
  dw=dw(:)'; 
  % Convert dw to a column vector (colum by column) and convert to a row vector
  bw=bb';
  bw=bw(:)'; % Data sequence samples
  w=sqrt(2*Eb/T)*cos(2*pi*fc*t); % carrier waveform
  bpsk_w=bw.*w; % modulated waveform
  
  % plotting commands follow
  
  subplot(4,1,1);
  plot(t,dw); axis([0 5 -1.5 1.5])
  
  subplot(4,1,2);
  plot(t,bw); axis([0 5 -1.5 1.5])
  
  subplot(4,1,3);
  plot(t,w); axis([0 5 -1.5 1.5])
  
  subplot(4,1,4);
  plot(t,bpsk_w,'.'); axis([0 5 -1.5 1.5])
  xlabel('time')
  </code></pre>
  
  <p>Which gives me the graphs shown below:</p>
  
  <p><img src="https://i.stack.imgur.com/5hdCw.png" alt="screenshot of BPSK waveforms"></p>
  
  <p>Below is my converted Python Code using Numpy / Scipy</p>
  
  <pre><code>import numpy as np
  import scipy
  import matplotlib.pylab as plt
  plt.clf()
  plt.close('all')
  
  d = np.array(np.hstack((1, 0, 1, 1, 0)))
  b = 2*d-1.
  T = 1
  Eb = T/2
  fc = 3/T
  t = np.linspace(0, 5, 1000)
  N = t.shape
  Nsb = np.divide(N, d.shape)
  dd = np.tile(d.conj().T, Nsb)
  bb = np.tile(b.conj().T, Nsb)
  dw = dd.conj().T
  dw = dw.flatten(0).conj()
  bw = bb.conj().T
  bw = bw.flatten(0).conj()
  w = np.dot(np.sqrt(np.divide(2*Eb, T)), np.cos(np.dot(np.dot(2*np.pi, fc), t)))
  bpsk_w = bw*w
  plt.subplot(4, 1, 1)
  plt.plot(t, dw)
  plt.axis(np.array(np.hstack((0, 5, -1.5, 1.5))))
  plt.subplot(4, 1, 2)
  plt.plot(t, bw)
  plt.axis(np.array(np.hstack((0, 5, -1.5, 1.5))))
  plt.subplot(4, 1, 3)
  plt.plot(t, w)
  plt.axis(np.array(np.hstack((0, 5, -1.5, 1.5))))
  plt.subplot(4, 1, 4)
  plt.plot(t, bpsk_w, '.')
  plt.axis(np.array(np.hstack((0, 5, -1.5, 1.5))))
  plt.xlabel('time')
  plt.show()
  </code></pre>
  
  <p>But I neither get an error nor the proper output:</p>
  
  <p><img src="https://i.stack.imgur.com/pDfCp.png" alt="screenshot of incorrect BPSK waveforms"></p>
  
  <p>Please let me know where is my error in migrating this code?</p>
  
  <p><strong>=====UPDATE======</strong></p>
  
  <p>When I change the Python code to use the following lines, I get some better output:</p>
  
  <pre><code>..............
  b = 2.*d-1.
  T = 1.
  Eb = T/2.
  fc = 3./T
  ...............
  w = np.dot(np.sqrt(np.divide(2.*Eb, T)), np.cos(np.dot(np.dot(2.*np.pi, fc), t)))
  .............
  </code></pre>