 ZGainsforth
9/4/2014 - 11:55 PM

## Subtract a single scattering profile from a monochromated diffraction pattern and compute the RDF from it.

Subtract a single scattering profile from a monochromated diffraction pattern and compute the RDF from it.

``````# -*- coding: utf-8 -*-
from matplotlib import *
use('Qt4Agg')
from matplotlib.pyplot import *
from numpy import *

def PlotRFFT(x, y):
### PlotRFFT(x, y): plots the FFT of y assuming y is real.  Generates one plot with two subfigures: abscissa = frequency and abscissa = period.

f = fft.rfft(y)/len(y)*2
nu = fft.rfftfreq(len(y), x-x)
p = 1/nu
amp = abs(f)

subplot(2,1,1)
plot(nu, amp)
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(p, amp)
xlabel('Period')
ylabel('Amplitude')
show()

close('all')
# Load the Q integration from disk.
Q = genfromtxt('Cecil Grid A6 S2 - 0053 - Q integration.dat')

# Simulate a single scattering profile (atomic form factor) as being propotional to Q**-2 and then fit it.
S = Q[:,0]**-2
# In this case I just fit it by hand.
Q2 = Q[:,1]-(S*7800+5300)

# Don't use the start or end of the vector -- it has artifacts.
iStart = 5
iEnd = -20
# Plot the patterns.
plot(Q[iStart:iEnd,0], Q[iStart:iEnd,1], Q[iStart:iEnd,0], S[iStart:iEnd]*7800+5300, Q[iStart:iEnd,0], Q2[iStart:iEnd])
xlabel('Q')
legend(['Raw', 'Q^-2 scattering background', 'Subtracted'])

# Plot the fft.
figure(2)
PlotRFFT(Q[iStart:iEnd,0], Q2[iStart:iEnd])

# Save the single scattering subtracted profile.
savetxt('Cecil Grid A6 S2 - 0053 - Q integration - form factor subtracted.dat', vstack((Q[iStart:iEnd,0], Q2[iStart:iEnd])).T)
``````