ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • deepseek-r1 on Jetson Orin Nano Super FFT Demo 1
    카테고리 없음 2025. 2. 8. 01:43

    deepseek-r1 / 7.6B/ Jetson Orin Nano Super

    Prompt

    fast fourier transformation (FFT)에 대해 이해하고 싶습니다. 진폭과 주파수가 다른 sine과 cosine 웨이브들을 각각 5개씩 더한 입력 데이터를 만들고, 이 데이터를 FFT하여 결과에서 입력한 진폭과 주파수를 확인하는 파이썬 예제를 부탁합니다. 그래프는 plotly로 해주세요.

    Response

    Jetson Orin Nano Super에 보관되어 있던 대화가 사라졌다.

    코드

    import numpy as np
    import plotly.graph_objects as go
    
    # Parameters
    sample_rate = 1000  # Sampling rate in Hz
    duration = 2  # Duration in seconds
    n_samples = sample_rate * duration
    
    # Time array from 0 to 2*pi
    t = np.linspace(0, 2*np.pi, n_samples)
    
    # Generate signals with different amplitudes and frequencies for sine and cosine
    np.random.seed(42)  # For reproducibility
    
    def generate_signal(frequency):
        amplitude = np.random.uniform(0.1, 1)
        return amplitude * np.sin(t * frequency), amplitude
    
    def generate_cosine_signal(frequency):
        amplitude = np.random.uniform(0.1, 1)
        return amplitude * np.cos(t * frequency), amplitude
    
    # Generate 5 sine and 5 cosine signals with different frequencies
    input_signals = []
    sine_amps = []
    cosine_amps = []
    
    for i in range(1, 6):
        sine_wave, amp = generate_signal(i)
        cosine_wave, cos_amp = generate_cosine_signal(i)
    
        input_signals.append(sine_wave + cosine_wave)
        sine_amps.append((i, amp))
        cosine_amps.append((i, cos_amp))
    
    # Combine all signals
    input_signal = np.sum(input_signals, axis=0)
    
    # Compute FFT
    fft = np.fft.fft(input_signal)
    magnitude = np.abs(fft)
    frequency = np.fft.fftfreq(n_samples) * sample_rate
    
    # Create time domain figure
    time_trace = go.Scatter(x=t, y=input_signal,
                             name='Time Domain Signal',
                             mode='lines')
    
    # Create frequency domain figure
    freq_trace = go.Scatter(x=frequency[:n_samples//2], 
                            y=magnitude[:n_samples//2],
                            name='Frequency Spectrum',
                            mode='lines')
    
    sine_traces = []
    cosine_traces = []
    
    for freq, amp in sine_amps:
        x = np.fft.fftfreq(n_samples) * sample_rate
        y = amp * np.sqrt(2)
        trace = go.Scatter(x=x[x > 0], y=y[:n_samples//2], 
                           name=f'Sine Component (f={freq}, A={amp})',
                           line=dict(color='red'))
        sine_traces.append(trace)
    
    for freq, cos_amp in cosine_amps:
        x = np.fft.fftfreq(n_samples) * sample_rate
        y = cos_amp * np.sqrt(2)
        trace = go.Scatter(x=x[x > 0], y=y[:n_samples//2], 
                           name=f'Cosine Component (f={freq}, A={cos_amp})',
                           line=dict(color='blue'))
        cosine_traces.append(trace)
    
    # Create layout
    fig_time = go.Layout(
        title='Time Domain Signal',
        xaxis_title='Time',
        yaxis_title='Amplitude',
        showlegend=True,
        hovermode='x unified'
    )
    fig_freq = go.Layout(
        title='Frequency Spectrum',
        xaxis_title='Frequency (Hz)',
        yaxis_title='Magnitude',
        hovermode='x unified',
    )
    
    # Create time domain figure with traces
    time_fig = go.Figure(time_trace, layout=fig_time)
    
    # Add sine and cosine components to frequency figure
    freq_fig = go.Figure()
    for trace in freq_traces:
        freq_fig.add_trace(trace)
    
    # Update frequency figure
    freq_fig.update_layout(hovermode='x unified')
    freq_fig.update_xaxes(tickangle=-45, tickformat='.1f')
    freq_fig.update_yaxis(type='log')
    
    # Show plots
    time_fig.show()
    freq_fig.show()
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    Cell In[2], line 61
         59     x = np.fft.fftfreq(n_samples) * sample_rate
         60     y = amp * np.sqrt(2)
    ---> 61     trace = go.Scatter(x=x[x > 0], y=y[:n_samples//2], 
         62                        name=f'Sine Component (f={freq}, A={amp})',
         63                        line=dict(color='red'))
         64     sine_traces.append(trace)
         66 for freq, cos_amp in cosine_amps:
    
    IndexError: invalid index to scalar variable.

    커멘트

    • 디버깅하지 않았다.