def plot_spectrum(self, ax=None, tickfmt=None, ntraces=10, fontsize=10): """ Plot a power spectrum. w is window length for smoothing filter """ if ax is None: fig = plt.figure(figsize=(12,6)) ax = fig.add_subplot(111) trace_indices = utils.get_trace_indices(self.data.shape[0], ntraces, random=True) fs = 1 / self.dt specs, peaks, mis, mas = [], [], [], [] for ti in trace_indices: trace = self.data[ti, :] if sum(trace) == 0: continue f, amp, fmi, fma = self.spectrum(trace, fs) peak = f[np.argmax(amp)] specs.append(amp) peaks.append(peak) mis.append(fmi) mas.append(fma) spec = np.mean(np.dstack(specs), axis=-1) spec = np.squeeze(spec) db = 20 * np.log10(amp) db = db - np.amax(db) f_peak = np.mean(peaks) f_min = np.amin(mis) f_max = np.amax(mas) statstring = "\nMin: {:.2f} Hz\nPeak: {:.2f} Hz\nMax: {:.2f}" stats = statstring.format(f_min, f_peak, f_max) ax.plot(f, db, lw=0) # Plot invisible line to get the min y_min = ax.get_yticks()[0] ax.fill_between(f, y_min, db, lw=0, facecolor='k', alpha=0.5) ax.set_xlabel('frequency [Hz]', fontsize=fontsize - 4) ax.xaxis.set_label_coords(0.5, -0.12) ax.set_xlim([0, np.amax(f)]) ax.set_xticklabels(ax.get_xticks(), fontsize=fontsize - 4) ax.set_yticklabels(ax.get_yticks(), fontsize=fontsize - 4) ax.set_ylabel('power [dB]', fontsize=fontsize - 4) ax.text(.98, .95, 'AMPLITUDE SPECTRUM'+stats, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes, fontsize=fontsize - 3) ax.yaxis.set_major_formatter(tickfmt) ax.xaxis.set_major_formatter(tickfmt) ax.grid('on') return ax
def plot_spectrum(spec_ax, data, dt, tickfmt, ntraces=10, fontsize=10): """ Plot a power spectrum. w is window length for smoothing filter """ trace_indices = utils.get_trace_indices(data.shape[1], ntraces, random=True) fs = 1/(dt/10**6) specs, peaks, mis, mas = [], [], [], [] for ti in trace_indices: trace = data[:, ti] f, amp, fmi, fma = get_spectrum(trace, fs) peak = f[np.argmax(amp)] specs.append(amp) peaks.append(peak) mis.append(fmi) mas.append(fma) spec = np.mean(np.dstack(specs), axis=-1) spec = np.squeeze(spec) db = 20 * np.log10(amp) db = db - np.amax(db) f_peak = np.mean(peaks) f_min = np.amin(mis) f_max = np.amax(mas) statstring = "\nMin: {:.2f} Hz\nPeak: {:.2f} Hz\nMax: {:.2f}" stats = statstring.format(f_min, f_peak, f_max) spec_ax.plot(f, db, lw=0) # Plot invisible line to get the min y_min = spec_ax.get_yticks()[0] spec_ax.fill_between(f, y_min, db, lw=0, facecolor='k', alpha=0.5) spec_ax.set_xlabel('frequency [Hz]', fontsize=fontsize - 4) spec_ax.xaxis.set_label_coords(0.5, -0.12) spec_ax.set_xlim([0, np.amax(f)]) spec_ax.set_xticklabels(spec_ax.get_xticks(), fontsize=fontsize - 4) spec_ax.set_yticklabels(spec_ax.get_yticks(), fontsize=fontsize - 4) spec_ax.set_ylabel('power [dB]', fontsize=fontsize - 4) spec_ax.text(.98, .95, 'AMPLITUDE SPECTRUM'+stats, horizontalalignment='right', verticalalignment='top', transform=spec_ax.transAxes, fontsize=fontsize - 3) spec_ax.yaxis.set_major_formatter(tickfmt) spec_ax.xaxis.set_major_formatter(tickfmt) spec_ax.grid('on') return spec_ax
def plot_spectrum(self, ax=None, tickfmt=None, ntraces=20, fontsize=10, colour='k'): """ Plot a power spectrum. w is window length for smoothing filter """ if tickfmt is None: # Set the tickformat. tickfmt = mtick.FormatStrFormatter('%.0f') if ax is None: fig = plt.figure(figsize=(12, 6)) ax = fig.add_subplot(111) trace_indices = utils.get_trace_indices(self.data.shape[:-1], ntraces, random=True) fs = 1 / self.dt specs, peaks, mis, mas = [], [], [], [] for ti in trace_indices: try: # 3D trace = self.data[ti[0], ti[1], :] except IndexError: # 2D trace = self.data[ti, :] if sum(trace) == 0: continue f, amp, fmi, fma = self.spectrum(trace, fs) peak = f[np.argmax(amp)] specs.append(amp) peaks.append(peak) mis.append(fmi) mas.append(fma) spec = np.nanmean(np.dstack(specs), axis=-1) spec = np.squeeze(spec) db = 20 * np.log10(spec) db = db - np.amax(db) f_peak = np.mean(peaks) f_min = np.amin(mis) f_max = np.amax(mas) dt = 1000 // fs f_nyquist = fs // 2 statstring = "\nMin: {:.1f} Hz\nPeak: {:.1f} Hz\nMax: {:.1f} Hz\nNyquist ({} ms): {} Hz" stats = statstring.format(f_min, f_peak, f_max, dt, f_nyquist) ax.plot(f, db, lw=0) # Plot invisible line to get the min y_min = ax.get_yticks()[0] ax.fill_between(f, y_min, db, lw=0, facecolor=colour, alpha=0.6) ax.set_xlabel('frequency [Hz]', fontsize=fontsize - 4) ax.xaxis.set_label_coords(0.5, -0.12) ax.set_xlim([0, np.amax(f)]) ax.set_xticklabels(ax.get_xticks(), fontsize=fontsize - 4) ax.set_yticklabels(ax.get_yticks(), fontsize=fontsize - 4) ax.set_ylabel('power [dB]', fontsize=fontsize - 4) ax.text(.98, .95, 'AMPLITUDE SPECTRUM', horizontalalignment='right', verticalalignment='top', fontweight='bold', color=colour, transform=ax.transAxes, fontsize=fontsize - 3) ax.text(.98, .95, stats, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes, fontsize=fontsize - 3) ax.yaxis.set_major_formatter(tickfmt) ax.xaxis.set_major_formatter(tickfmt) ax.grid() gridlines = ax.get_xgridlines() + ax.get_ygridlines() for line in gridlines: line.set_linestyle('-') line.set_alpha(0.2) return ax