Or the bioconductor limma package if you are dealing with arrays and/or RNA-Seq to analyze your data
Limma will give you the log2 expression changes based upon statistical values
You could reinvent the wheel of course, but If you ask such a question, use what pros have put a lot of thought in:
Fold change can also be computed in unsupervised fashion, where we don't know the class labels(like case-control or type1-type2) of the samples. In that setting we can use mean expression of a gene as the base value and compute the fold change for that gene in each sample.